FizzBuzz is not printing ( "Introduction to Objects I")


#1


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

"FizzBuzz" if not printing even though 15 is divisible by 3 and 5.


var numbers = [0]; 

for(var i = 1; i < 21; i++) { 
   
     numbers.push(i);
     console.log(numbers[i]);
     
     if( numbers[i] % 3 === 0) {
         console.log("Fizz");
         }
         else if (numbers[i] % 5 === 0) { 
             console.log("Buzz");
             } 
             else if ((numbers[i] % 5 === 0) && (numbers[i] % 3 === 0)) { 
                 console.log("FizzBuzz");
                 }
                 }


#2

When numbers[i] is 3, it prints "Fizz" and continues to a new iteration. Same when numbers[i] is 5. The combined case is never seen. That case should be examined first.

You are taking a long way around by using an array. The for loop already counts from 1 to 20. Why not just use i instead of numbers[i]?


#3

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