3
\$\begingroup\$

This is a simple JavaScript Fizz Buzz solution based on a Codecademy exercise. I refactored it down a few lines after seeing these implementations.

I wanted to post here and see what else I can learn from this basic exercise.

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);  
}
\$\endgroup\$
0

2 Answers 2

4
\$\begingroup\$

A standard FizzBuzz runs from 1 to 100.

Your solution is otherwise fine. It's standard enough that it ended up being nearly identical to code that someone else wrote. We could suggest ideas, but they wouldn't necessarily be improvements.

\$\endgroup\$
0
2
\$\begingroup\$

It's a small detail, but in one of your if you're doing = and in the other one +=.

It works, but what you are saying is that when i % 5 = 0 it could be a regular assignation of Buzz or it could be the addition of Buzz to Fizz.

I notice it because you have two different ways of working with the string result. It's a small details and you don't really need to change it, but those are things you need to be aware of. I'm a bit paranoid and it's a big extrapolation, but when a program (other than a FizzBuzz) will change those difference have a chance to impact the development.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.