Created
September 22, 2016 01:46
-
-
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.
This file contains hidden or 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
//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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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