FizzBuzz - Solution comparison

Exercize: FizzBuzz (https://eloquentjavascript.net/02_program_structure.html)

Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.

When you have that working, modify your program to print "FizzBuzz" for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those).

My Solution:

for (number = 1; number <= 100; number++) {
    if (number % 3 == 0 && number % 5 == 0) {
        console.log('FizzBuzz');
	}
    else if (number % 3 == 0) {
        console.log("Fizz");	
	}
    else if (number % 5 == 0) {
        console.log("Buzz");
	}
    else {
    	console.log(number);
	} 
}

Textbook Solution

for (let n = 1; n <= 100; n++) {
  let output = "";
  if (n % 3 == 0) output += "Fizz";
  if (n % 5 == 0) output += "Buzz";
  console.log(output || n);
}

Is my answer incorrect? (even though it produces expected outcome)
What are the takeaway’s from this exercise? What is emphasized in an exercise like this?
Are beginners expected to write minimal solutions like the answer shows?

Your solution is not incorrect since it produces the same result. Yet the textbook solution is more compact. It only proves there are multiple roads leading to Rome.

You will encounter examples like this a lot in your coding career. Eventually you will learn tricks to make code more compact.

1 Like

One problem I see is that you didn’t declare your variable “number”.

for (number = 1; number <= 100; number++)

vs

for (let number = 1; number <= 100; number++)

Usually, if the code works correctly, there is some other problem they are checking (sometimes it’s little things like spaces, which is annoying).

But I believe the courses try to hammer in declaring variables with let or const when they are first introduced, even if most javascript code will run either way.

Cheers!

1 Like

Declaring variables wit let or const may seem trivial, but there is good reason to not do this:
for(number = 1; number <=100; number++). Declaring a variable without using var, let or const gives the variable global scope. In the context of a simple program, it may not ever matter, but developing a habit of giving global scope to every variable is very bad practice.
Consider:

for (number = 1; number <= 15; number++) { //changed 100 to 15
    if (number % 3 == 0 && number % 5 == 0) {
        console.log('FizzBuzz');
	}
    else if (number % 3 == 0) {
        console.log("Fizz");	
	}
    else if (number % 5 == 0) {
        console.log("Buzz");
	}
    else {
    	console.log(number);
	} 
}

let number = 25
console.log(number)

Seems like it should work, right?
Output:

ReferenceError: number is not defined

Comment out the let number = 25:

for (number = 1; number <= 15; number++) {
    if (number % 3 == 0 && number % 5 == 0) {
        console.log('FizzBuzz');
	}
    else if (number % 3 == 0) {
        console.log("Fizz");	
	}
    else if (number % 5 == 0) {
        console.log("Buzz");
	}
    else {
    	console.log(number);
	} 
}

//let number = 25
console.log(number)

Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16

Notice the 16 at the end. We still have access to number because of the global scope. If we use let in the for statement, we limit the scope to the for loop’s block, and we don’t have to deal with the perhaps unanticipated behavior previously shown:

for (let number = 1; number <= 15; number++) {
    if (number % 3 == 0 && number % 5 == 0) {
        console.log('FizzBuzz');
	}
    else if (number % 3 == 0) {
        console.log("Fizz");	
	}
    else if (number % 5 == 0) {
        console.log("Buzz");
	}
    else {
    	console.log(number);
	} 
}

let number = 25
console.log(number)

Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
25

1 Like

I believe we are saying the same thing. I was trying to be succinct in describing why @adrianagarmo83689337 code will still run even though a strict language like c++ would never let it through.

Although, I have never personally tried to create this kind of error (if I forget to declare at the top, I wouldn’t think of doing it at the bottom). So you did teach me something!

Cheers!

(Haven’t yet I should say)

2 Likes

In a small program, there’s not really an issue, but if you had 100’s or 1000’s of lines of code, you could have a nightmare on your hands trying to figure out the errors caused by global variables.

1 Like

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