Task
You will be given an array of numbers. You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions.
Examples
[7, 1] => [1, 7]
[5, 8, 6, 3, 4] => [3, 8, 6, 5, 4]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] => [1, 8, 3, 6, 5, 4, 7, 2, 9, 0]
<code>
function sortArray(array) {
const result = array.filter((a)=>
a%2 !== 0)
result.sort(function(a,b){
return a-b
});
console.log(result);
let count = 0;
const newarray = [];
for(let i=0;i<array.length;i++){
if(array[i]%2==0){
newarray[i]=array[i];
}
if(array[i]%2!==0){
newarray[i]=result[count];
count++;
}
}console.log(newarray)
return newarray
}
'Algorithm > Codewars' 카테고리의 다른 글
[Codewars/JS] RGB To Hex Conversion (0) | 2022.01.13 |
---|---|
[Codewars/JS] Two to One (0) | 2022.01.13 |
[Codewars/JS] Count the number of Duplicates (0) | 2022.01.12 |
[codewars/JS] Growth of a Population (0) | 2022.01.12 |
[codewars/JS] Sum of two lowest positive integers (0) | 2022.01.11 |