Created
February 21, 2015 19:18
-
-
Save angvishvish/063cad9fd7cc7a553829 to your computer and use it in GitHub Desktop.
Have the function DashInsert(str) insert dashes ('-') between each two odd numbers in str. For example: if str is 454793 the output should be 4547-9-3. Don't count zero as an odd number.
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
function DashInsert(str) { | |
str = str.split('').map(function (item) { | |
if (item%2 != 0) { | |
return item + "-"; | |
} | |
return item; | |
}).join(''); | |
str = str.split(''); | |
return str[str.length-1]%2 == 0 ? str.join(''): str.join('').substring(0, str.length - 1); | |
} | |
// keep this function call here | |
// to see how to enter arguments in JavaScript scroll down | |
DashInsert(readline()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment