And the good! help


#1


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


#2

Hi ftwda

you have to switch it around

function = FizzBuzz

to

FizzBuzz = function


#3

Alright so I've just done it right, there are lots of issues with it..
First, as the guy above me said, the syntax for writing a function is "var FizzBuzz = function() {
you've added a ";" after () which is incorrect, as your function is not over..
to the for, you started it from 0, while you had to start from 1 so the correct syntax is "for ( var i=1; i<21; i++ ) {}"
to the if statements, you should begin with the "&&" as it should first check if both conditions are met, before checking for if only one of them is met
which means you've to begin with %3 && %5
another mistake, "FizzBuzz" is not a number, it's the function's name while the number is "i" so you've to do "if ( i % 3 === 0 && i % 5 === 0 ) {
console.log("FizzBuzz")
}
same thing for the else if statements, you should replace the "FizzBuzz" with "i"
Full correct format:

var FizzBuzz = function() {
 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);
    }
 }
}
FizzBuzz();

#4

Thank u so much! :blush:


#5

also you can do the same with below code,

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

I have a quick question. Why does the | "FizzBuzz" if | have to come first in the code. I figured it out after some doing that this was the problem, but I don't understand why this code doesn't work and the following one does when they're essentially the same thing aside from the order.

//code1 wrong
var FizzBuzz = function () {
for (var i = 1; i <= 20; i = i + 1) {
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)
}
}
}
FizzBuzz()

//code 2 correct
var FizzBuzz = function () {
for (var i = 1; i <= 20; i = i + 1) {
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)
}
}
}
FizzBuzz()


#7

To understand that you'll need to think like the server does, if you don't put it first, you'll check for "Fizz" first, it'll consider every one that can be divided by 3 as Fizz and everyone divisible by 5 as Buzz. So after that when you give it the "FizzbUzz" option, it'll for sure not consider it, because the ones that accept FizzBuzz are already considered as Fizz becasue it is first
If you do not understand, let's say you're a human doing an exercice
The first question is to make the Fizz
Second is to make Buzz
then the third, is about FizzBuzz
by answering the first question, you'll make all numbers that can be divided by 3 Fizz
then in the second, the ones that can be divided by 5 will be Buzz
when it comes to the third, you'll find that the ones that you need to make "FizzBuzz" are already set to Fizz as it comes first. And this is the problem, another language might have other priorities and do this kind of code right, but javascript doesn't


#8

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