New here and been trying to figure this out for a bit now. Can't seem to find the answer.
Problem: trying to separate all numbers from 5 upwards into a separate array "bigNumbers". All other numbers to "smallNumbers"
Here's what I have so far:
let allNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let bigNumbers = [];
let smallNumbers = [];
allNumbers.forEach(function (a) {
if(allNumbers >= 5) {
return allNumbers.push(bigNumbers);
} else {
return allNumbers.push(smallNumbers);
}
});
Might be taking the wrong approach entirely here using the .push() method. Any feedback is appreciated.
allNumbers >= 5you are comparing an array to be greater than five. and then you haveallNumbers.push(bigNumbers);You are pushing thebigNumbersarray into theallNumbersarray. You are not pushing a value into the array. Learn basic debuggingconsole.log(allNumbers,5, allNumbers>=5)that will show you what is happening on the if.bigNumbers .push(a);andsmallNumbers.push(a)instead. and checkif (a >= 5)instead as well.forEach()callback you should be referring toa, notallNumbersreturnand also change the if to checkareturnis pointless because.forEach()ignores return values.