# Fizz Buzz Solution: Unexpected Token Function?

4120 views
0

I'm having some frustration with this code. I may not be seeing it. I keep getting either an "Unexpected Token" or "ILLEGAL" error (the latter which completely perplexed me, considering I've never seen an error like that before in my life.

I've been double checking the syntax and I'm thinking it may be something I'm just not catching?

``````function fizzBuzz(n) {
for (i = 0; i<=n; i++) {
if (n%3===0) {
print ("fizz")
}
if (n%5===0) {
print ("buzz")
}
if (i%3 !== 0 && i%5 !== 0) {
return [i];
}
}
}
fizzBuzz(100);``````

I'd be thankful for the help! <3

10

You need some changes:

• remove line numbers,

• check first for `'fizz buzz'` value and use `console.log` for output. Then use `continue` statement for jumping to top for next loop,

• use `i` instead of `n` for checking.

``````function fizzBuzz(n) {
for (i = 0; i <= n; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("fizz buzz");
continue;
}
if (i % 3 === 0) {
console.log("fizz");
continue;
}
if (i % 5 === 0) {
console.log("buzz");
continue;
}
console.log(i);
}
}

fizzBuzz(100);``````

posted this
0

``````function fizzBuzz(n) {
2    for (i = 0; i<=n; i++) {
3    if (n%3===0) {                You're checking if the maximum number (n) is divisible by 3.
4    print ("fizz")                You should be checking i.
5    }
6    if (n%5===0) {                You're checking if the maximum number (n) is divisible by 5.
7    print ("buzz")                You should be checking i.
8    }
9    if (i%3 !== 0 && i%5 !== 0) {
10      return [i];                You're returning an array with a single element: i.
11    }                            You should print i instead.
12    }
13    }
14
15
16    fizzBuzz(100);
``````

The line numbers are what's causing the "ILLEGAL" error. Remove them.

`print` in JavaScript opens the print dialog to literally print paper out of a printer. Use `console.log`

If a number is divisible by both 3 and 5, `fizzbuzz` should be printed. Your code prints `fizz` and `buzz` on separate lines.

Working version ( https://www.youtube.com/watch?v=QPZ0pIK_wsc ) :

``````function fizzBuzz(n) {
for (var i = 0; i <= n; i++) {
var printVal = "";
if (i % 3 === 0) {
printVal += "fizz";
}
if (i % 5 === 0) {
printVal += "buzz";
}
if (printVal === "") {
printVal = i;
}
console.log(printVal);
}
}

fizzBuzz(100);``````

posted this