CodeWars | Sort the odd

by Sebastian Kay

JavaScript

function sortArray(array) {
	let arrayOdd = [];
	let arrayNew = [];
	let j = 0;
	
	for (let i in array) if (i % 2 == 0) arrayOdd.push(array[i]), arrayOdd.sort((a, b) => {return (a===0)-(b===0) || +(a>b)||-(a<b);});
	
	for (let i in array) {
		if (i % 2 == 0) arrayNew.push(arrayOdd[j]), j++;
		if (i % 2 == 1) arrayNew.push(array[i]);
	}
	return arrayNew;
}

const Test1 = sortArray([5, 3, 2, 8, 1, 4]);
const Test2 = sortArray([5, 3, 1, 8, 0]);

console.log("Test1: " + Test1);
console.log("Test2: " + Test2);