Reputation: 630
This was my attempt at writing a loop that loops through nums, if the item is even, it adds it to the evens array, if the item is odd, it adds it to the odds array.
var nums = [1,2,34,54,55,34,32,11,19,17,54,66,13];
var evens = [];
var odds = [];
var sorter = function() {
for (var i = 0; i < nums.length; i++) {
if (nums[i]%2 !== 0) {
odds.push(i);
}
else {
evens.push(i);
}
}
};
sorter();
console.log(evens);
console.log(odds);
Upvotes: 0
Views: 98
Reputation: 1069
The problem is that you are pushing your iterator to the arrays. You should do this:
var nums = [1,2,34,54,55,34,32,11,19,17,54,66,13];
var evens = [];
var odds = [];
var sorter = function() {
for (var i = 0; i < nums.length; i++) {
if (nums[i]%2 !== 0) {
odds.push(nums[i]);
}
else {
evens.push(nums[i]);
}
}
};
sorter();
console.log(evens);
console.log(odds);
See fiddle here
Upvotes: 2