Skip to content

Instantly share code, notes, and snippets.

@angvishvish
Created February 21, 2015 19:18
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 angvishvish/063cad9fd7cc7a553829 to your computer and use it in GitHub Desktop.
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.
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