Created
January 18, 2014 17:20
-
-
Save areagray/8493459 to your computer and use it in GitHub Desktop.
Coderbyte: Have the function DashInsert(num) insert dashes ('-') between each two odd numbers in num. For example: if num is 454793 the output should be 4547-9-3. Don't count zero as an odd number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//coderbyte | |
function DashInsert(num) { | |
var prev='', | |
newstring=''; | |
num = num.toString(); | |
for (var i=0; i<num.length; i++){ | |
parseInt(num[i])%2 == 0 ? current='even' : current='odd'; | |
if (current=='odd' && prev=='odd'){ | |
newstring=newstring + '-' + num[i]; | |
prev='odd'; | |
} else { | |
newstring=newstring + num[i]; | |
prev=current; | |
} | |
} | |
return newstring; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment