Profile image of jastuccio
Submitted by jastuccio
almost 13 years

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

Profile image of crushy77
Submitted by crushy77
over 12 years

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.”

Profile image of mark_bueno
Submitted by mark_bueno
over 12 years

1 comments

Profile image of jastuccio
Submitted by jastuccio
over 12 years

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++);
Profile image of jastuccio
Submitted by jastuccio
almost 13 years

4 comments

Profile image of mahamdu27yahoo.in
Submitted by mahamdu27yahoo.in
over 12 years

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

Profile image of jastuccio
Submitted by jastuccio
over 12 years

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?

Profile image of mahamdu27yahoo.in
Submitted by mahamdu27yahoo.in
over 12 years

yep….thanks a lot…

Profile image of coursePro15849
Submitted by coursePro15849
over 12 years

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 “”.

Profile image of anonymous
Submitted by anonymous
over 12 years

2 comments

Profile image of jastuccio
Submitted by jastuccio
over 12 years

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.

Profile image of anonymous
Submitted by anonymous
over 12 years

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); }

Profile image of methodSurfer47897
Submitted by methodSurfer47897
almost 12 years

Popular free courses

  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      4 Lessons
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      11 Lessons
  • Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
    • Beginner Friendly.
      6 Lessons
Explore full catalog