Why is this not working?


#1

https://www.codecademy.com/courses/spencer-sandbox/0/3?curriculum_id=506324b3a7dffd00020bf661


The instructions basically say:
Print out the numbers from 1 - 20.
The rules:
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
Otherwise, just print out the number.

I am getting an error message that says I printed out the wrong number of items... This might be because it logs 'undefined' at the end?

Here is my code:


// Numbers
var number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

// Conditional statements
for (var i = 0; i <= 20; i++ < 21) {
    if (number[i] % 3 === 0 && number[i] % 5 === 0) {
        console.log("FizzBuzz");
    } else if (number[i] % 5 === 0) {
        console.log("Buzz");
    } else if (number[i] % 3 === 0) {
        console.log("Fizz");
    } else {
        console.log(number[i]);
    }
}


#2

Problem1
for (var i = 0; i <= 20; i++ < 21)
i++ < 21?

Last part of for loop includes how your loop will work by increment/decrement!

problem2.
in for loop you need to change the mid condition as i <= 19

i <= 19 (previously it was creating out of range error)


#3

This is a sequence, which is made redundant by the for loop which iterates a sequence, already.

for (var i = 1; i < 21; i++) {
    if (i % ...) {

    } // ...
}

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.