Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Отсортировать только нечетные элементы массива, четные оставить на своих местах
- [ 2, 3, 7, 4, 6, 1, 5, 8, 9 ] => [2, 1, 3, 4, 6, 5, 7, 8, 9]
- */
- function sort(arr) {
- let ind = [];
- let odd = [];
- arr.forEach((el,i) => {
- if (el % 2 !== 0) {
- ind.push(i);
- odd.push(el)
- }
- })
- odd.sort((a,b) => a -b)
- odd.forEach((el, i) => {
- arr[ind[i]] = el;
- })
- return arr;
- }
- console.log(sort([ 2, 3, 7, 4, 6, 1, 5, 8, 9 ]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement