Skip to content

Instantly share code, notes, and snippets.

@mmloveaa mmloveaa/Insert Dash

Created Jan 10, 2016
Embed
What would you like to do?
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
You can’t perform that action at this time.