Skip to content

Instantly share code, notes, and snippets.

@ryunp
Created September 22, 2016 01:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryunp/9119529bfd2f714bf87306b55e7afbe8 to your computer and use it in GitHub Desktop.
Save ryunp/9119529bfd2f714bf87306b55e7afbe8 to your computer and use it in GitHub Desktop.
Codewars.com JS 'kata': Sort ascending odd numbers but even numbers must be on their places.
//TASK
You have an array of numbers.
Your task is to sort ascending odd numbers but even numbers must be on their places.
Zero isn't an odd number and you don't need to move it. If you have an empty array, you need to return it.
//EXAMPLE
sortArray([5, 3, 2, 8, 1, 4]) == [1, 3, 2, 8, 5, 4]
// SOLUTION
function sortArray(array) {
var odd = array.filter(is_odd).sort(ascending);
return array.map(replace_odd_inorder);
function ascending(a, b) {
return a > b;
}
function is_odd(num) {
return num % 2;
}
function replace_odd_inorder(num) {
return is_odd(num) ? odd.shift() : num;
}
}
@zikaloai71
Copy link

the ascending function has no importance and it doesn't make the code work so instead, you can keep it and make it return a-b or do all this in the body of sort function itself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment