如何将任何可被3整除的数字添加到新数组中?

时间:2016-06-23 22:40:25

标签: javascript

我有一个函数,它接收一个充满数字的数组。它应循环遍历数组,但我希望它将任何可被3整除的数字添加到一个名为three的新数组中。一旦发生,我只想返回三个数组。这是我到目前为止所拥有的。

var numbers = [1, 2, 3, 4, 5, 6, 7];

function loveTheThrees(numeros) {
  for(var i = 0; i < numeros.length; i++)
}
  var threes;
  //%3
  return threes;
var ok = loveTheThrees(numbers);

//line 1 shows the array numbers
//line 3 I have written a function called loveTheThrees
//line 4 is looping through the array but I want to add any numbers that are divisible by 3 to a new array called threes
//I also want to return the threes array after I have added all the numbers that are divisble by 3

1 个答案:

答案 0 :(得分:-1)

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

Ok现在是一个可被3整除的所有数字的数组。