Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Created January 10, 2016 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmloveaa/8fbd699beedb1bc115c8 to your computer and use it in GitHub Desktop.
Save mmloveaa/8fbd699beedb1bc115c8 to your computer and use it in GitHub Desktop.
Insert Dash
// 1/10/2016
// Write a function insertDash(num) that will 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.
// My Solution:
function insertDash(num) {
var arr1=num.toString().split("")
console.log(arr1)
for (var i=0; i<arr1.length; i++){
if ((arr1[i]%2===1) && (arr1[i+1]%2===1)){
arr1.splice(i+1,0,"-")
}
console.log(arr1)
}
arr1=arr1.join("")
return arr1
}
insertDash(454793)
// Test.assertEquals(insertDash(454793),'4547-9-3');
// Test.assertEquals(insertDash(123456),'123456');
// Test.assertEquals(insertDash(1003567),'1003-567');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment