1

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 preplexed 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) {
2    for (i = 0; i<=n; i++) {
3    if (n%3===0) {
4    print ("fizz")
5    }
6    if (n%5===0) {
7    print ("buzz")
8    }
9    if (i%3 !== 0 && i%5 !== 0) {
10      return [i];
11    }
12    }
13    }
14    
15    
16    fizzBuzz(100);

I'd be thankful for the help! <3

clickbait
  • 2,818
  • 1
  • 25
  • 61
Adèle B.
  • 19
  • 2

4 Answers4

0

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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);
clickbait
  • 2,818
  • 1
  • 25
  • 61
0

Lets decrease number of if statements, I have seen many fizzbuzzs - I would like to try another approach

const fizzBuzz = (i = 0, ret) => {
  while (++i, ret = '', i <= 100) {
    if (i % 3 === 0) {
      ret += `fizz`
    }
    if (i % 5 === 0) {
      ret += `buzz`
    }
    console.log(ret || i)
  }
}

fizzBuzz()

The most important part is console.log(ret || i) - empty string is falsy value, so console will log ret if there is some value or current i value

Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
0

If you're trying to print to your browser's console, use console.log() instead of print(), since print() will open the printer preview in a browser. I'm using Chrome. Remember to check the console by pressing F12 and clicking on "console" in case you've created a html file that includes your javascript script.

Since you are iterating i until it reaches the value of n, you if statements should look like:

if(i%3===0){ // your code... }

Based on your code, it seems like you want to check for a value that is not a multiple of 3 && a multiple of 5. However, the fizzbuzz challenge requires to print fizzbuzz when encountering a number that is both a multiple of 3 && a multiple of 5. So, you'll need to add the following condition:

       if (i%3 === 0 && i%5 === 0) {
            console.log("fizzbuzz");
            continue; /* Continue may or may not be necessary depending on your program's logic. Continue allows to re-enter the loop 
without checking for the remaining if statements. If you use continue, this condition must be the first if statement inside the loop */
            } 

Your function doesn't need to return anything, since you're using console.log() to visualize the values in each loop iteration. That's why you need to change return [i] for console.log(i).

Don't give up and keep trying!

If you have further questions, I'll be glad to help you.

Jorge Garcia
  • 16
  • 1
  • 2