Created
January 10, 2016 18:44
-
-
Save mmloveaa/8fbd699beedb1bc115c8 to your computer and use it in GitHub Desktop.
Insert Dash
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
// 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