No FizzBuzz


#1


The code below checks each element in the array for the "Fizz" and "Buzz" numbers and prints out all the right things but it prints out "Fizz" instead of "FizzBuzz" when it checks 15.

Plz Help....

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


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

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

}


#2

well for 15, this condition:

if ( 15 % 3 === 0 )

is true, so Fizz is logged, when if evaluate to true, else if and else are skipped and the loop moves on to the next number

checking if a number is fizzbuzz should be your first order of operation


#3

Additionally, your code could be simplified.

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

for (var i = 0; i < numbers.length; i++) {...}

Remember that a for loop includes an initial expression, a condition, and an increment. You don't even need the var numbers at all.


#4

Yeah if the first condition is satisfied, the loop moves on to the next number. Got it. Thanks. :thumbsup:


#5

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