# 3.And the good; I have a error

var Divisible =
for (i=0;i<= 20; i++) {
console.log(Divisible[i])
if (Divisible[i] % 3 === 0) {
console.log(“Fizz”
}
else if (Divisible[i] % 5 === 0) {
console.log(“Buzz”)
}
else if (Divisible[i] % 3 === 0 && Divisiblei[i] % 5 === 0) {
console.log(“FizzBuzz”)}
else{
console.log(“i”)
}

}
What wrong with this code?

This is your code with proper formatting:

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

}
``````

Problem should be obvious now, but if it’s not -> you should close your `consoel.log()` statement.

You know that `Divisible` is an empty array? `Divisible[3]` is not defined.

There is also a problem in logic of your program, but I think this is not a matter of this thread.

2 Likes

By the way, I think it makes more sense to put :

``````else if (Divisible[i] % 3 === 0 && Divisiblei[i] % 5 === 0) {
console.log("FizzBuzz")
``````

to be the first equation because if code evaluates that number is divisible by 3 it will output “Fizz” even if the number is divisible by 5 too.

3 Likes

Yes, this is the logic problem I mentioned.

If someone is struggling with the syntax error, it’s usually a bad idea to correct also logic problems for him. Give them a chance

Haha yeah I get you but code won’t work them good after they found the 1. error.
Kill 2 flies in 1 shot

3 Likes

for (var i = 1; i < 21; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log(“FizzBuzz”);
} else if (i % 3 === 0) {
console.log(“Fizz”);
} else if (i % 5 === 0) {
console.log(“Buzz”);
} else {
console.log(i);
}
}

I think it should be like this…

7 Likes

How does the logic work here? I started the If…else with (i % 3 === 0) instead of (i % 3 === 0 && i % 5 === 0). Why didn’t it work?

I think you should put first condition:
`if (i % 3 === 0 && i % 5 === 0)`
then the rest of condition betwen () as in my example.

I think this is more mathematical and more logic.
But this is only my opinion, this syntax works for me

@luciranger2:
It didn’t work because if you have 3 if statements if one of them is true it doesn’t evaluate the rest.

For example for the number 15, which is divisible by 3 and 5, in this case if the ( i % 3 ===0) is first , it will evaluate it true and run it. It will not go down to evaluate (i % 3 === 0 && i % 5 === 0). Thus (i % 3 === 0 && i % 5 === 0) needs to be first, or use another looped if statement to check for this condition after checking the first one.

3 Likes

Here’s my code:

`for (var i = 1; i <= 20; i+=1){ if ((i % 3 === 0) && (i % 5 === 0)){ console.log("FizzBuzz"); } else if (i % 5 === 0){ console.log("Buzz"); } else if (i % 3 === 0){ console.log("Fizz"); } else { console.log(i); }; };`
Notice how I did not use an unnecessary variable such as your divisible array. The exercise is asking for you to create a simple for loop with an if else statement. Set your “FizzBuzz” else if statement at the top as the computer gets confused with the other two else if statements before it. I don’t know why nor am I a professional, so please don’t ask me.

4 Likes

I think the main issue with your code is that you start your for loop from 0 instead of 1, and the instruction is ‘Print out the numbers from 1 - 20’ . The var Divisible is also unnecessary since if( i%3 ===0) should work just fine.

1 Like

Thanks man it worked… And i’ve found my mistake in my code

1 Like

btw u don`t need double parenthesis. It will work even with code like this:
if (i%3 === 0 && i%5 === 0)

2 Likes

first, i wrote “for”,
and then if/else 3% && 5%,
and then…3% or other …

gj~

1 Like

for(var i =0; i<=20; i++){
console.log(i);
if(i % 3 == 0){
console.log(“Bizz”);
}else if((i % 5 === 0){
console.log(“Fizz”);
}else if(i % 3 === 0 && i % 5 === 0){
console.log(“FizzBuzz”);
}else{
console.log(i);
}
}

What’s wrong here

just a couple of typos:

``````for (var i =0; i<=20; i++)
{
console.log(i);
if(i % 3 === 0)
{
console.log("Bizz");
}
else if(i % 5 === 0)
{
console.log("Fizz");
}
else if(i % 3 === 0 && i % 5 === 0)
{
console.log("FizzBuzz");

}
else
{
console.log(i);

}
}
``````

}else if((i % 5 === 0){

but I also noticed you had missed off an equals sign:

if(i % 3 == 0){

also just so you know - if you want to copy code into forum so its laid out like I have done just add three back ticks ``` before the code and three back ticks after the code

``````code
``````

back ticks ``` can be found next to left shift key if using mac or if I recall correctly near to/ directly next to the numerical no. 1 if using windows

just a suggestion - as it will help others read code better.

Why did mine not work?

for (i = 1; i >= 20; i++){
if (i % 5 === 0 && i % 3 === 0){
console.log(“FizzBuzz”);
} else if (i % 5 === 0 ){
consloe.log(“Buzz”);
}
else if (i % 3 === 0 ){
consloe.log(“Fizz”);
}
} else {
console.log(i);
}
};

1 Like

Its i <= 20 (smaller or equal) not >=. Your i will never +1 because condition is false.
Also fix console.log typo with Buzz and Fizz.

1 Like

Hi and you should remove the `}` before the `else statement` because its close the for loop.

1 Like

Hello,
I seem to have an error with my code: “Oops, try again. It looks like you printed out the wrong number of items.”
`for (var i = 0; i < 20; i++) { if ((i % 3 === 0) && (i % 5 === 0)) { console.log("FizzBuzz"); } else { if (i % 3 === 0) { console.log("Fizz"); } if (i % 5 === 0) { console.log("Buzz"); } } }`

1 Like