 # Javascript for loop problem

i came across this problem: Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print “Fizz” instead of the number, and for numbers divisible by 5 (and not 3), print “Buzz” instead. When you have that working, modify your program to print “FizzBuzz” for numbers that are divisible by both 3 and 5 (and still print “Fizz” or “Buzz” for numbers divisible by only one of those).

and i tried solving it with the code below:

``````for(let i = 1; i <= 100; i++){
if(i % 3 ===0) {
console.log("fizz");
} else if ( i % 5 === 0 ) {
console.log("buzz");
} else if (i % 5 === 0 && i % 3 === 0) {
console.log("fizzbuzz");
}
console.log(i);
}
type or paste code here
``````

please can anyone tell me what i did wrong because i am not getting result

It looks like the Fizz Buzz version worked for you and you are trying to get FizzBuzz to work?

If that’s the case, then your problem is that the condition in your second else-if clause will never be reached. You will want to modify the first two clauses (or restructure your code) so that Fizz or Buzz is not printed when i is a multiple of both 3 and 5.

thank you for your prompt response, i tried this :

``````for(let i = 1; i <= 100; i++){
if(i % 3 ===0) {
i ="fizz";
} else if ( i % 5 === 0 ) {
i = "buzz";
} else if (i % 5 === 0 || i % 3 === 0) {
i = "fizzbuzz";
}
console.log(i);
}`
``````

but still not getting result

It does not look like anything changed?

`if (i % 3 === 0)` will be true when i is divisible by 3, regardless of if i is divisible by 5, so `if ((i % 3 === 0) || (i % 5 === 0))` will never be reached.

can you help me correct it please

you need to change the order

remember that the first if statement that is true is the one executed