...And the Good


#1



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


Unexpected token )


var num =1
for (num=1, num<21,num++){
    if(num % 3===0){
    if(num % 5===0){
        console.log("FizzBuzz")}
        else{
            console.log("Fizz")}
    else if(num%5===0){
        console.log("Buzz")}
        else{
            console.log(num)
        }
    }


#2

Thank you for any help in advance! I know there are probably a lot of errors, but I'm trying my best.


#3

var num =1
for (num=1, num<21,num++){
if(num % 3===0){
if(num % 5===0){
console.log("FizzBuzz")}
else{
console.log("Fizz")}}
else{
if(num%5===0){
console.log("Buzz")}
else{
console.log(num)
}
}}
This is the new version.


#4

Hi inside your dor loop

for (num=1, num<21,num++)

you should separate each statement with ; instead of ,

for (statement1; statement; statement) {

}

#5

Hello guys,

Here is my solution, maybe someone finds it helpful.

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)
}
}


#6

@netwhiz13363 This solution works..But why we put the condition of
if (i % 3 === 0 && i % 5 === 0){
console.log ("FizzBuzz")
}
first before the other two conditions??

This solution should work as well but it doesn't!!
for (var i=1; i<21; i++)
{
if (i%3===0 )
{
console.log("Fizz");
}
else if(i%5 === 0)
{
console.log("Buzz");
}
else if (i%3===0 && i%5 === 0)
{
console.log("FizzBuzz");
}
else
{
console.log(i);
}
}
can you explain why??


#7

Your solution is 100% correct but for some reason you need to make it this way:

for (var i=1; i<21; i++)
{
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);
}
}

This condition (i%3===0 && i%5 === 0) has to be placed in front of the others because that one will be checked first, If no criteria is met, that is if the first if statement is false then computer will check other two to see if the numbers meet given conditions.We use else if to specify a new condition to test, if the first condition is false.

In your case, since the first condition was true then else if statement for FizzBuzz wasn't even tested, Testing will be like this:
1. First check if the condition one is true/false. If it's true that means that specific number will be "Fizz". Let;s say for number 1, first condition is false, system will check the second condition, since the second condition is flase, system will porceed with the third condition. Third condition is false and for that matter else statement is executed and you see the number 1. ( console.log(i) ).

  1. Second, system will check if the second condition is tru/false. Since for number 15 second condition is true that means that system will not even check the third one, so the code from the second one will be executed and that number will be marked as Buzz. That's why it failed.

You code was written perfectly, just the order of the conditions was not right.

If you have any further questions feel free to ask, I hope I helped you a bit.

Thank you.


#8

Thanks for your reply and for your support!!


#9

Thanks.This works well.