Skip to content

Instantly share code, notes, and snippets.

@rvbsanjose
Last active August 29, 2015 13:59
Show Gist options
  • Save rvbsanjose/10708035 to your computer and use it in GitHub Desktop.
Save rvbsanjose/10708035 to your computer and use it in GitHub Desktop.
// Using the JavaScript language, 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.
// Input = 99946 Output = "9-9-946"
// Input = 56730 Output = "567-30"
function DashInsert( num ) {
var dashI = '';
var nums = ( num ).toString().split( '' );
for ( var i = 0; i < nums.length; i++ ) {
if ( ( nums[i] % 2 != 0 ) && ( nums[i+1] % 2 != 0 ) && ( nums[i+1] != undefined ) )
dashI += nums[i] + '-' ;
else
dashI += nums[i];
}
return dashI;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment