I can't get a result I wanted:
Even Array is: 2, 18, 38, -10, 0, 104
Odd Array is: 3, 13, -5, 11
Two arrays where one contains even numbers another odd.
Here is the code:
let arrayMain = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
let oddArray = [];
let evenArray = [];
for (let i = 0; i < arrayMain.length; i++){
if (i % 2 == 0) {
evenArray.push(arrayMain[i]);
}
else {
oddArray.push(arrayMain[i]);
}
}
console.log("Odd Array is " + oddArray);
console.log("Even Array is " + evenArray);
It gives me:
Odd Array is 3,18,38,11,104
Even Array is 2,13,-5,-10,0
How can I fix this?
iis the counteri % 2will divide divide the array into odd and even places, not odd and even values. You need to doarrayMain[i] % 2debugger;inside the code and verify each value and condition.