How to push elements from one array into another array in JavaScript? -


this code:

function divisibleby(numbers, divisor){     var mytext = numbers;     var myarray = [];      for(var = 0; < mytext.length; i++){         if(mytext[i] % divisor === 0){             myarray.push(mytext[i]);             console.log(myarray);         }     } }  divisibleby([1,2,3,4,5,6,7,8,9,10,11,12],3); 

when console.log, expect see: [3,6,9,12] instead get:

[ 3 ] [ 3, 6 ] [ 3, 6, 9 ] [ 3, 6, 9, 12 ] 

even worse, when put return myarray instead of console.log(myarray);i see [3] can please explain going wrong?

thank in advance help.

just log outside loop.

function divisibleby(numbers, divisor) {     var mytext = numbers;     var myarray = [];     (var = 0; < mytext.length; i++) {         if (mytext[i] % divisor === 0) {             myarray.push(mytext[i]);         }     }     console.log(myarray); } divisibleby([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 3); 

Comments