This forum is now read-only. Please use our new forums! Go to forums
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); }
- start the loop
- create a variable to hold Fizz, Buzz, or the number. It also resets the variable at the start of each loop
- change result to Fizz for multiples of 3
- APPEND Buzz using += to the result for multiples of 5
- If the result is still “” that means it was not divisible by 3 or 5, change result to the variable i ( the number)
- 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
Answer 52b7aa1552f863b90800316d
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.”
Answer 52ae87257c82ca58c700c802
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++);
4 comments

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

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?

yep….thanks a lot…

very nice !
Answer 52c483ad282ae3b8ed00514a
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 “”.
2 comments

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.
Popular free courses
- Free Course
Learn SQL
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 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
Thanks Mark. Its good you realized that result is just a variable I created. I could have named it anything.