Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

return new array that is divisible by 3

so here is the prompt,,, Write a function called iLoveThree that takes in an array full of numbers. It should loop through the array and adds any numbers that are divisible by 3 to a new array called threes. Once you have added all the numbers that are divisible by 3 to the new array, then return the threes array.

here is my answer

function iLoveThree(){
    var array = [];
    var threes = [];
  for (var i = 0; i < array.length; i++) {
        if (array[i] % 3 == 0) {
      threes.push(array[i]);
    }
  }
    return threes;
}

what I did wrong?

1 Answer

You have to add numbers to the array variable for example

var array = [1, 2, 3];

From the numbers in this array the number three would be placed into the threes array as it is divisible by three.

if you had

var array = [3, 6, 9]; 

the numbers 3, 6 and 9 would be placed inside the threes array and displayed and so on.