# LC 101, Ch. 9 For Loop Exercise #3

Hi guys, I’m having trouble with Ch. 9 For Loop Exercise #3 which asks:

Construct a for loop that sorts the array [2, 3, 13, 18, -5, 38, -10, 11, 0, 104] into two new arrays, one that contains the even numbers, and the other holds the odds.

My code is working, however it seems to be evaluating the index numbers in the array (i.e. 0-9) rather than the actual numbers (2, 3, 13. etc.) as my output shows two new arrays: [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9].

I’ve tried adding in Num to the code to see if that would fix it and I’ve also tried googling “javascript for loop evaluating index of array rather than number” but I’m not having a lightbulb moment. Wondering if you all have any ideas?

Without seeing your code I am not sure I can help address the specific issue you’re having.
Essentially I did a for loop & nested an if statement inside, as the loop happens it evaluates the values - if value is odd it’ll push to odds if even it pushes to evens.
IDK how you set up your loop because going through the loop should evaluate the values. Please post your code so I can help point you to a fix. I can also post my solution if that helps. Let me know!

Hi Steph, thanks so much but I think I just figured it out! I hadn’t added the array name and brackets (e.g. otherArray[i]) around the i variable, and that fixed it!

1 Like

i’m having issues on the same exercise, i’m stuck trying to figure out how to get the loop to break my code into odd/evens and add them to the new arrays, my code is as follows:
let arr = [2,3,13,18,-5,38,-10,11,0,104];

let evens = []

let odds = []

for (let i = 0; i < arr.length; i++){

evens += i;

odds += i;

}

console.log(evens)

console.log(odds)

Note: I got it now - don’t forget to push (thanks @StephNau)!
My code is working and I also used a for loop & nested an if (plus else) statement inside - but my output shows empty items like this for evens:
[2, <2 empty items>, 18, <1 empty item>, 38, -10, <1 empty item>, 0, 104]

How do I get rid of the empty items? Here’s my if/else:
if ((otherarray[u]%2)===0){
evens[u] = otherarray[u];
} else {
odds[u] = otherarray[u];
}
Any help would be appreciated.

Hello emgraham, if you wouldn’t mind sharing your code i’d be very grateful, i am completely stuck on how to add values to the new arrays. I’m getting nowhere with the following code:
let arr = [2,3,13,18,-5,38,-10,11,0,104];
let evens = []
let odds = []
for (let i = 0; i < arr.length; i++){
evens.push(arr[i]%0)
odds.push(arr[i]%1)
}
console.log(evens)
console.log(odds)

This is what I have (please consider o to be i):

let list = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
let evens = [];
let odds = [];

for(o=0; o<list.length;o++){
if(list[o]%2===0){
evens.push(list[o]);
} else {
odds.push(list[o]);
}
}
console.log(evens);
console.log(odds);

2 Likes

Thank you Steph! That was very helpful!

1 Like

THANK YOU!! StephNau

1 Like

Hey @bradjhaley, sorry - only seeing your message now! Thanks also @StephNau - my code is the exact same as yours (once I remembered to add in the indices like I mentioned before)

1 Like

@emgraham, no worries at all, i think we’re operating on different time schedules.