Skip to Content
Delete Icon
This forum is now read-only. Please use our new forums at discuss.codecademy.com.
0 points
Submitted by Joe
7 years ago

FizzBuzz in 6 lines! What do you think of my solution?

Tonight I refactored my code and I really like what I came up with. I picked up the append to idea in a tutorial somewhere. One of my professors said that its usually better not to repeat math operations. This solution only divides by 3 and 5 once each. My original solution divided by 3 AND 5 twice each.

I hope it doesn’t come across as bragging. Ive been studying programming for awhile and feel like it is finally starting to make sense :) Id like to see a “best solutions” section on the forum. I learned a lot by looking at other peoples code.

  for (var i = 1; i <= 20; i++) {
        var result = "";
        if (i % 3 === 0)    { result = "Fizz"; }
        if (i % 5 === 0)    { result += "Buzz"; }
        if (result === "")  { result = i; }
        console.log(result); }

  1. start the loop
  2. create a variable to hold Fizz, Buzz, or the number. It also resets the variable at the start of each loop
  3. change result to Fizz for multiples of 3
  4. APPEND Buzz using += to the result for multiples of 5
  5. If the result is still “” that means it was not divisible by 3 or 5, change result to the variable i ( the number)
  6. console log the result variable

*** REFACTORED! Now one line shorter. ***

for (var i = 1; i <= 20; i++) { var result = “”; if (i % 3 === 0) { result = “Fizz”; } if (i % 5 === 0) { result += “Buzz”; } console.log(result || i); }


I deleted line 5 if (result === “”) { result = i; }

and added ‘|| i’ to what was line 6. I saw this used in someone else solution and was going to link to it, but I lost the page.
console.log(result || i);

Who knew id learn so much from FizzBuzz. There is even a video about TDD (test driven development) and FizzBuzz http://goo.gl/gbkhkX. I will be watching that later to see what else I can learn.

What I am excited about is programming is starting to make sense to me. I can read other peoples code then improve my code based on what I learned!

Answer 52aee0ba52f863604d0003ba

1 vote

Permalink

This is amazing i couldn’t have seen this simple way

421 points
Submitted by crushy77
7 years ago

Answer 52b7aa1552f863b90800316d

1 vote

Permalink

this is startlingly brilliant. just wow. i had no idea there was even a “result” command, much less that it could be used like this. I also didn’t realize you could stack “if” statements like that.

mind === blown.

EDIT: ignore my comment about “return.” I just realized that where I’m using “i” you’re using “return.”

564 points
Submitted by Mark Bonneaux
7 years ago

1 comments

Joe 7 years ago

Thanks Mark. Its good you realized that result is just a variable I created. I could have named it anything.

Answer 52ae87257c82ca58c700c802

0 votes

Permalink

A quick check on google shows im not even close to the smallest code. Oh well…. http://rosettacode.org/wiki/FizzBuzz#Alternative_version_.28one-liner.29

/* Alternative version (one-liner) */
for (var i=1; i<=100; i++) console.log( (i % 3 === 0 ? 'Fizz' : '') + (i % 5 === 0 ? 'Buzz' : '') || i );

/* Bodyless for loop */
for(var i=1; i<=100; console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i), i++);
1275 points
Submitted by Joe
7 years ago

4 comments

Mahalingam S 7 years ago

“APPEND Buzz using += to the result for multiples of 5” hi…joe can you explain this step.

Joe 7 years ago

It is shorthand code for: result = result + “Buzz”;

like when you use: i += 1;

Instead of overwriting the result variable it adds the string “Buzz” to either “” or “Fizz” depending on if the number was multiple of 3. Does that explanation help?

Mahalingam S 7 years ago

yep….thanks a lot…

alberto arranz 7 years ago

very nice !

Answer 52c483ad282ae3b8ed00514a

0 votes

Permalink

for (i = 1; i <= 20; i++) { var result = “”; if (i % 3 === 0) { result = “Fizz”; } if (i % 5 === 0) { result += “Buzz”; } console.log(result || i); } …….. Can someone interpret line 2 in an English sentence for me please? I understand that the line declares “result” a new variable, but I don’t quite understand the meaning of “”.

906 points
7 years ago

2 comments

Joe 7 years ago

Good question tsb. I had to read about null vs undefined because I was not 100% sure what I had done. I think Line 2 initializes the variable result with a value of null. In line 5 if result is null it logs whatever number i is in the loop instead.

Thanks for the timely reply, Joe. I’m still not sure I fully grasp “”. But at least I know now to read up on null vs. undefined.

Answer 5437ff2c631fe9ecfd000261

0 votes

Permalink

Try this

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

537 points
Submitted by M Maina
6 years ago