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 origin
Table of contents
No headings in the article.
function sortArray(array){
//contain odd numbers only
const oddNumbers = array.filter(num => num % 2 !== 0).sort((a,b) => a-b);
//keeps track of the index of the next odd number in the 'oddNumbers' array
let oddIndex = 0;
return array.map(num => {
if (num % 2 !==0) {
cost sortedOdd = oddNumbers[oddIndex];
oddIndex++
return sortedOdd
} else {
return num
}
})
Questions that I needed further clarification with:
How do the even numbers remain in their original position:?
The even numbers remain in their original positions because of the way the 'map()' method works. The 'map()' method creates a new array by applying the given function to each element of the input array in order, maintaining the same index positions as the original elements.
In the given code, the function provided to 'map()' checks if the current number (num) is odd or even. If it's even (num % 2 === 0), the function simply returns the original even number without making any changes to it. Since 'map()' creates the new array while preserving the index positions, the even numbers in the output array are automatically placed in the same positions as in the input array.
Here's a brief example to illustrate this:
Input array: [5, 8, 3, 2, 7]
The 'map()' method starts iterating through the input array.
For the first element (5), it is odd. The code proceeds to replace it with the next sorted odd number.
For the second element (8), it is even. The code just returns 8 unchanged. In the output array, 8 will be placed in the same position (index 1) as in the input array.
The process continues for the rest of the elements.
In the end, the output array will look like this: [3, 8, 5, 2, 7]
As you can see, the even numbers 8 and 2 remain in their original positions (indexes 1 and 3) in the output array.
const sortedOdd = oddNumbers[oddIndex]
The line const sortedOdd = oddNumbers[oddIndex];
retrieves the sorted odd number at the current position indicated by oddIndex
.
oddNumbers
is an array containing all the odd numbers from the input array, sorted in ascending order. oddIndex
is a variable that starts at 0 and is incremented each time an odd number is processed by the 'map()' method.
When the 'map()' method iterates through the input array and encounters an odd number, this line of code retrieves the corresponding sorted odd number from the oddNumbers
array using the value of oddIndex
. This sorted odd number will replace the original odd number in the output array.
For example, consider the following input array: [5, 8, 3, 2, 7]
The 'oddNumbers' array is created with sorted odd numbers: [3, 5, 7]
'oddIndex' starts at 0.
When the 'map()' method encounters an odd number (e.g., 5), it retrieves the sorted odd number from the 'oddNumbers' array at position 'oddIndex' (0 in this case), which is 3.
The original odd number (5) is replaced by the sorted odd number (3) in the output array.
'oddIndex' is incremented by 1 for the next odd number.
By repeating this process for all odd numbers in the input array, the output array will have the odd numbers sorted in ascending order while keeping the even numbers in their original positions.