Javascript loop an array to find numbers divisible by 3

前端 未结 8 1162
清歌不尽
清歌不尽 2021-01-25 05:41

I am needing to find the correct way to have javascript loop through an array, find all numbers that are divisible by 3, and push those numbers into a new array.

Here is

8条回答
  •  旧时难觅i
    2021-01-25 06:15

    var originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    function loveTheThrees(array1) {
      var threes = [];
      for (var i = 0; i < array1.length; i++) {
        if (array1[i] % 3 === 0) {
          threes.push(array1[i]);
        }
      }
      return threes;
    }
    loveTheThrees(originalArray);
    

提交回复
热议问题