Skip to content

Instantly share code, notes, and snippets.

@areagray
Created January 18, 2014 17:20
Show Gist options
  • Save areagray/8493459 to your computer and use it in GitHub Desktop.
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.
//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