I'd like to see if this is a reasonable solution, or is just plain terrible.
I wouldn't say it is "terrible" - mostly because it works and doesn't appear to be very inefficient. However, there are some improvements that can be made.
- use strict equality comparison - i.e.
===when comparing values. That way it won't need to convert the types. - Use consistent indentation for readability The first and last lines within the
forloop are not indented, while the other lines between them are, though maybe it was the case that your code was indented properly but when you pasted here it was thrown off because of the markdown formatting... - Statements and blocks - For the sake of readability, expand blocks so each statement is on a separate line and terminate statements with a semi-colon. The first three conditional blocks don't have a semi-colon after the statement within the block, while the last one does.
###Updated Code Consider the modified code below, utilizing the feedback above.
Note: the inline console in the snippets is truncated to ~50 lines, but the complete console log should be visible in your browser console.
for (var i = 1; i <= 100; i++) {
var output = i;
if(i % 15 == 0) {
output = "FizzBuzz";
}
else if(i % 3 == 0) {
output = "Fizz";
}
else if(i % 5 == 0) {
output = "Buzz";
}
console.log(output);
}