Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a function that pulls all number from an array divisible by 3

var array = [3, 4, 6, 5, 9, 10, 21];

var divThree = [];

var loveTheThrees = function (array) {
  for (i = 0; i = array.length; i++) {
    if (array[i] % 3 === 0) {
      amount.push(divThree[i]);
    }
  }
}

I am just learning Javascript and having a bit of trouble with this function. I can feel im close but cant figure out what im doing wrong.

like image 751
Josh-Harris Avatar asked Feb 17 '26 14:02

Josh-Harris


1 Answers

Just use filter()

divThree = array.filter(x => x%3 === 0);
like image 81
kevin ternet Avatar answered Feb 19 '26 04:02

kevin ternet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!