3/33. ... And the Good!


#1


We have range in Python, which is very useful in such situations.
What is the workaround for this in Javascript?
Pretty sure i can make it work with a usual array and for loop for that array, but what is the shortest and neatest way?



SyntaxError: Unexpected token {
Oops, try again. There was a problem with your syntax.

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


#2

if else is improper syntax. It's else if

Hope I Helped :slight_smile:


#3

i see syntax errors without even running the code:

var FizzBuzz = function

function() requires parentheses.

if else? that one is new. seems you swapped something

a number divisible by 3 and 5 (for example 15) is also divisible by either one:

if(15 % 3 === 0)

this is true, so if will execute, this means else if and else will never execute, so to check if a number is fizzbuzz should be the first condition

why do you use var keywords in the conditions? You use a variable in the conditions, you are not declaring a variable

your loop:

for (var numbers = 0, numbers < 21, i++)

the declaration, condition and increment should separated with semi-colons, not comma's (,)

dont log the number if you need to log fizz, buzz or fizzbuzz


#4

Thank you for a valuable reply, it really helped a lot.
I just resumed my course and didn't remember the rules of JavaScript, your advice about semicolons was successfully applied)
Yeah, sorry for if else, copy paste is bad for me)


#5

good you got the issue resolved :slight_smile:

posting answers like that is not according to the guidelines, i removed it.


#6