dimanche 3 juillet 2016

How to place the results of an if statement into a new array which I can return later

I'm fairly new to code and I've been stuck on one particular problem for quite some time. I feel I have a good grasp on what's needed for the problem, but can't figure out for the life of me how to return the results of a if statement into a new array which I can then return. Any help or advice would be greatly appreciated.

function loveTheThrees (myArray) {

var myTotal = 0;

for (var i = 0; i < myArray.length; i++) {
  if (myArray[i] % 3 === 0) {
    myTotal += myArray[i];
    /* What I'm looking to do at this stage of the problem is place the results into a new array which will be returned rather than myTotal */
}
}

return myTotal; // Placed this here just to test to see if the problem would post the results
}

loveTheThrees ([1, 3, 5, 12, 21]);

Aucun commentaire:

Enregistrer un commentaire