FizzBuzz in Javascript
I'm sure everyone here knows what FizzBuzz is. I would like constructive criticism for my solution.
I'm a beginner to programming as a whole and this isn't my first solution, but it's what I think is my best solution. I'd like to see if this is a reasonable solution, or is just plain terrible.
for(var i=1;i<=100;i++) {
var output = "";
if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else {output = i;}
console.log(output);
}
The output is correct. It's your standard FizzBuzz output.
javascript beginner fizzbuzz iteration
migrated from stackoverflow.com 2 days ago
This question came from our site for professional and enthusiast programmers.
add a comment |
I'm sure everyone here knows what FizzBuzz is. I would like constructive criticism for my solution.
I'm a beginner to programming as a whole and this isn't my first solution, but it's what I think is my best solution. I'd like to see if this is a reasonable solution, or is just plain terrible.
for(var i=1;i<=100;i++) {
var output = "";
if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else {output = i;}
console.log(output);
}
The output is correct. It's your standard FizzBuzz output.
javascript beginner fizzbuzz iteration
migrated from stackoverflow.com 2 days ago
This question came from our site for professional and enthusiast programmers.
4
It's hard to give constructive criticism on such a small, simple function. It's a reasonable solution and the only things I could pick out being wrong would be me being extremely picky or a personal preference.
– George
2 days ago
4
You could go withvar output = i;
and then remove that finalelse
block.
– user189829
2 days ago
One possible issue is you are relying on a standard fizzbuzz using 3 and 5 (hence checking for 3 * 5 as 15). Someone might give you a test using 2 and 4 to see if you fell into the trap of checking for 8.
– Kickstart
yesterday
It would be better to include the actual spec you are following. The FizzBuzz solution looks fine. Any extra points goes to how you follow the nuance of the spec. If you really wanted to get fancy, you could do it without the explicit "FizzBuzz" part of the algorithm.
– Tombo
yesterday
add a comment |
I'm sure everyone here knows what FizzBuzz is. I would like constructive criticism for my solution.
I'm a beginner to programming as a whole and this isn't my first solution, but it's what I think is my best solution. I'd like to see if this is a reasonable solution, or is just plain terrible.
for(var i=1;i<=100;i++) {
var output = "";
if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else {output = i;}
console.log(output);
}
The output is correct. It's your standard FizzBuzz output.
javascript beginner fizzbuzz iteration
I'm sure everyone here knows what FizzBuzz is. I would like constructive criticism for my solution.
I'm a beginner to programming as a whole and this isn't my first solution, but it's what I think is my best solution. I'd like to see if this is a reasonable solution, or is just plain terrible.
for(var i=1;i<=100;i++) {
var output = "";
if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else {output = i;}
console.log(output);
}
The output is correct. It's your standard FizzBuzz output.
javascript beginner fizzbuzz iteration
javascript beginner fizzbuzz iteration
edited 11 hours ago
Pedro A
1334
1334
asked 2 days ago
Zachary WoodsZachary Woods
7415
7415
migrated from stackoverflow.com 2 days ago
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com 2 days ago
This question came from our site for professional and enthusiast programmers.
4
It's hard to give constructive criticism on such a small, simple function. It's a reasonable solution and the only things I could pick out being wrong would be me being extremely picky or a personal preference.
– George
2 days ago
4
You could go withvar output = i;
and then remove that finalelse
block.
– user189829
2 days ago
One possible issue is you are relying on a standard fizzbuzz using 3 and 5 (hence checking for 3 * 5 as 15). Someone might give you a test using 2 and 4 to see if you fell into the trap of checking for 8.
– Kickstart
yesterday
It would be better to include the actual spec you are following. The FizzBuzz solution looks fine. Any extra points goes to how you follow the nuance of the spec. If you really wanted to get fancy, you could do it without the explicit "FizzBuzz" part of the algorithm.
– Tombo
yesterday
add a comment |
4
It's hard to give constructive criticism on such a small, simple function. It's a reasonable solution and the only things I could pick out being wrong would be me being extremely picky or a personal preference.
– George
2 days ago
4
You could go withvar output = i;
and then remove that finalelse
block.
– user189829
2 days ago
One possible issue is you are relying on a standard fizzbuzz using 3 and 5 (hence checking for 3 * 5 as 15). Someone might give you a test using 2 and 4 to see if you fell into the trap of checking for 8.
– Kickstart
yesterday
It would be better to include the actual spec you are following. The FizzBuzz solution looks fine. Any extra points goes to how you follow the nuance of the spec. If you really wanted to get fancy, you could do it without the explicit "FizzBuzz" part of the algorithm.
– Tombo
yesterday
4
4
It's hard to give constructive criticism on such a small, simple function. It's a reasonable solution and the only things I could pick out being wrong would be me being extremely picky or a personal preference.
– George
2 days ago
It's hard to give constructive criticism on such a small, simple function. It's a reasonable solution and the only things I could pick out being wrong would be me being extremely picky or a personal preference.
– George
2 days ago
4
4
You could go with
var output = i;
and then remove that final else
block.– user189829
2 days ago
You could go with
var output = i;
and then remove that final else
block.– user189829
2 days ago
One possible issue is you are relying on a standard fizzbuzz using 3 and 5 (hence checking for 3 * 5 as 15). Someone might give you a test using 2 and 4 to see if you fell into the trap of checking for 8.
– Kickstart
yesterday
One possible issue is you are relying on a standard fizzbuzz using 3 and 5 (hence checking for 3 * 5 as 15). Someone might give you a test using 2 and 4 to see if you fell into the trap of checking for 8.
– Kickstart
yesterday
It would be better to include the actual spec you are following. The FizzBuzz solution looks fine. Any extra points goes to how you follow the nuance of the spec. If you really wanted to get fancy, you could do it without the explicit "FizzBuzz" part of the algorithm.
– Tombo
yesterday
It would be better to include the actual spec you are following. The FizzBuzz solution looks fine. Any extra points goes to how you follow the nuance of the spec. If you really wanted to get fancy, you could do it without the explicit "FizzBuzz" part of the algorithm.
– Tombo
yesterday
add a comment |
6 Answers
6
active
oldest
votes
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.
Style Guide Consider following a style guide. Many common style guides advise separating keywords with a space - e.g.if (
instead ofif(
.
Use consistent indentation The first and last lines within thefor
loop 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 - If you are going to use block statements then expand them so each statement is on a separate line. Otherwise, just put the single statement without a block. Also use consistent statement termination. The first three conditional blocks don't have a semi-colon after the statement within the block, while the last one (in theelse
block) does.
Consider default valueoutput
is never left as""
because it is always set in one of theif
,else if
orelse
blocks. Theelse
could be eliminated ifoutput
is assigned toi
. In a strongly typed language this might have ramifications but not in JavaScript.
Abstract logic into a function As Paul's answer suggests: you can put the core logic into a function that returns the output but doesn't handle outputting the value (e.g. to the console). This allows such code to be atomic and testable - congruent with the Single Responsibility Principle. Also, thereturn
statement can eliminate the need forelse
keywords within a function. One drawback is that calling a function on each iteration may slow down operations but should be negligible for a set of numbers 1 through 100.
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.
function fizzBuzz(value) {
if (value % 15 === 0) return "FizzBuzz";
if (value % 3 === 0) return "Fizz";
if (value % 5 === 0) return "Buzz";
return value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
One option I did consider is to minimize the number of modulo operations, append to the output string instead of outputting it. If you are trying to optimize for speed, this might not be the approach to take because appending to the string may be much slower than doing an extra couple modulo operations. Try a comparison in this jsPerf test.
function fizzBuzz(value) {
var output = "";
if (value % 3 === 0) output += "Fizz";
if (value % 5 === 0) output += "Buzz";
return output || value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
3
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
2 days ago
4
@CaseyB I don't think arguing about micro-optimizations like this when OP is a beginner is a good idea at all.
– Voile
yesterday
3
@Voile One million percent. Even if this were a production-ready, enterprise-grade, cloud-based microservice™, readability and maintainability is always the number one concern. The concept of micro-optimizing a JavaScript FizzBuzz is honestly hilarious.
– Michael
yesterday
6
@Michael FizzBuzz Enterprise Edition begs to differ. :P But FWIW, I believe Casey was merely providing a counterargument for the microoptimization that the answer itself first suggested.
– Mateen Ulhaq
yesterday
2
@MateenUlhaq 260 issues... man, they really need to refactor it, arrange UATs, and use Kanban.
– Juha Untinen
yesterday
|
show 5 more comments
One of the problems is that the case where you check i % 15
(i.e. i
is a multiple of 3 and 5) is unnecessary. You have the concept of 3 and 5 repeated, and the concept of Fizz and Buzz repeated.
This is not currently much of a problem but suppose someone asks you to extend your program to print "Jazz" when i
is a multiple of 7. Using your current strategy we now need a case where i
is a multiple of:
- 3
- 5
- 7
- 3 and 5
- 3 and 7
- 5 and 7
- 3 and 5 and 7
It would look something like this:
for(var i = 1;i <= 100; i++) {
var output = "";
if(i % 105 == 0) {output = "FizzBuzzJazz"}
else if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 35 == 0) {output = "BuzzJazz"}
else if(i % 21 == 0) {output = "FizzJazz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else if(i % 7 == 0) {output = "Jazz"}
else { output = i; }
console.log(output);
}
See how quickly that got out of hand? If we add a fourth word it becomes even worse.
If we use a different strategy by appending text to the output
variable, we can get away with having as few conditions as we have words.
for(var i = 1; i <= 100; i++) {
var output = "";
if (i % 3 === 0) {output += "Fizz";}
if (i % 5 === 0) {output += "Buzz";}
if (i % 7 === 0) {output += "Jazz";}
console.log(output === "" ? i : output);
}
(I've fixed a few other things as suggested in Sam's answer)
One thing that might be new to you here, if you're a beginner, is that the expression used as the argument for console.log
or called the conditional or ternary operator. Ours says that if the output is blank (i.e. not a multiple of 3, 5 or 7) then print i
, else print the string that we've compounded.
The ternary operator can always be replaced by an if-statement if you're not yet comfortable with it.
@PatrickRoberts True. I wanted to focus more on the approach to solving the problem than those things. Buuut I did correct everything else (I couldn't help myself) so yeah I've made that change as well. Thanks
– Michael
10 hours ago
1
You forgot the equality in the ternary expression. I tried to edit myself but I can only make edits of at least 6 characters :|
– Patrick Roberts
9 hours ago
add a comment |
I think it's fine as is, though some folks like to return early instead of using a series of if..else. For example:
function calc(i) {
if(i % 15 == 0) return "FizzBuzz";
if(i % 3 == 0) return "Fizz";
if(i % 5 == 0) return "Buzz";
return i;
}
for(var i=1;i<=100;i++) {
console.log(calc(i));
}
1
"Returning early" is often called "multiple exit points" and is sometimes considered poor practice.
– Moby Disk
yesterday
5
"Returning early" is often called "validation" and is often considered good practice.
– nurettin
14 hours ago
1
Returning early has many names and people get very worked up about whether their way is best.
– Tim B
8 hours ago
add a comment |
Expanding on Michael's answer, you could also create an object with all the words you want, with the key being the number your input must be divisible by, to make this more dynamic and a bit more future proof (A lot easier to add a new entry to the object versus adding more lines of code), and the object itself can also be dynamically generated.
divisions = {3: "Fizz", 5: "Buzz", 7: "Jazz"};
for(var i = 1; i <= 100; i++) {
var output = "";
for (var x in divisions) {
if(i % x == 0) output += divisions[x];
}
console.log(output == "" ? i : output);
}
add a comment |
From a maintenance stand point, I think it's better that you check for 3 and 5 instead of checking for 15. The issue with checking for 15 is that when you come back to your code 6 months from now, you won't realize that you were just printing fizzbuzz when something was divisible by both 3 and 5. That might matter in the future, but I think that's something worth talking about in an interview.
add a comment |
Though your code is fine, another thing to look is amount of division happening the code, Division is computationally expensive operation and as @Michael has suggested that it is redundant. So always try to minimise the multiplication and division operation in your code.
You have mentioned that you are a beginner programmer. I believe it is a good practice to start familiarising your self with topics like Computational Complexity.
Look here for computational complexity of mathematical functions
New contributor
While I think it's a bit early for a beginning programmer to be learning about things such as memory management (assuming this is one of their FIRST actual programs), I think this is a good answer!
– nostalgk
yesterday
2
This advice screams "premature optimization". Division is not an expensive operation. It's a bit more expensive that addition but it's not really expensive on today's computers. Your mobile phone can do millions of divisions in a fraction of a second. String concatenation is much more expensive, by far. The most important metric in code quality is readability. Also, if you are talking about computational complexity, you should note that your change won't change complexity at all.
– Sulthan
yesterday
The theoretical computational complexity of the underlying division algorithm is practically irrelevant.
– crasic
23 hours ago
@Sulthan The subject computational complexity doesn't take in to account how many operations a machine can perform, it takes into account how many operations/steps are required to solve a problem. While CC of addition i linear, division is quadratic. Dividing only by 3 and 5 would change the complexity and results of those can be saved in a bool variable which can be evaluated in constant time complexity.
– Hemanshu
12 hours ago
1
On a modern CPU integer division takes 1 CPU cycle, the JS version of%
a little longer. JS will use 32bit signed ints when it can so the operation is insignificant in comparison to just calling a function. BTW the linked computational complexity page has nothing to do with how CPUs ALU and FLU process the basic math operations.
– Blindman67
12 hours ago
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211113%2ffizzbuzz-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Style Guide Consider following a style guide. Many common style guides advise separating keywords with a space - e.g.if (
instead ofif(
.
Use consistent indentation The first and last lines within thefor
loop 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 - If you are going to use block statements then expand them so each statement is on a separate line. Otherwise, just put the single statement without a block. Also use consistent statement termination. The first three conditional blocks don't have a semi-colon after the statement within the block, while the last one (in theelse
block) does.
Consider default valueoutput
is never left as""
because it is always set in one of theif
,else if
orelse
blocks. Theelse
could be eliminated ifoutput
is assigned toi
. In a strongly typed language this might have ramifications but not in JavaScript.
Abstract logic into a function As Paul's answer suggests: you can put the core logic into a function that returns the output but doesn't handle outputting the value (e.g. to the console). This allows such code to be atomic and testable - congruent with the Single Responsibility Principle. Also, thereturn
statement can eliminate the need forelse
keywords within a function. One drawback is that calling a function on each iteration may slow down operations but should be negligible for a set of numbers 1 through 100.
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.
function fizzBuzz(value) {
if (value % 15 === 0) return "FizzBuzz";
if (value % 3 === 0) return "Fizz";
if (value % 5 === 0) return "Buzz";
return value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
One option I did consider is to minimize the number of modulo operations, append to the output string instead of outputting it. If you are trying to optimize for speed, this might not be the approach to take because appending to the string may be much slower than doing an extra couple modulo operations. Try a comparison in this jsPerf test.
function fizzBuzz(value) {
var output = "";
if (value % 3 === 0) output += "Fizz";
if (value % 5 === 0) output += "Buzz";
return output || value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
3
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
2 days ago
4
@CaseyB I don't think arguing about micro-optimizations like this when OP is a beginner is a good idea at all.
– Voile
yesterday
3
@Voile One million percent. Even if this were a production-ready, enterprise-grade, cloud-based microservice™, readability and maintainability is always the number one concern. The concept of micro-optimizing a JavaScript FizzBuzz is honestly hilarious.
– Michael
yesterday
6
@Michael FizzBuzz Enterprise Edition begs to differ. :P But FWIW, I believe Casey was merely providing a counterargument for the microoptimization that the answer itself first suggested.
– Mateen Ulhaq
yesterday
2
@MateenUlhaq 260 issues... man, they really need to refactor it, arrange UATs, and use Kanban.
– Juha Untinen
yesterday
|
show 5 more comments
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.
Style Guide Consider following a style guide. Many common style guides advise separating keywords with a space - e.g.if (
instead ofif(
.
Use consistent indentation The first and last lines within thefor
loop 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 - If you are going to use block statements then expand them so each statement is on a separate line. Otherwise, just put the single statement without a block. Also use consistent statement termination. The first three conditional blocks don't have a semi-colon after the statement within the block, while the last one (in theelse
block) does.
Consider default valueoutput
is never left as""
because it is always set in one of theif
,else if
orelse
blocks. Theelse
could be eliminated ifoutput
is assigned toi
. In a strongly typed language this might have ramifications but not in JavaScript.
Abstract logic into a function As Paul's answer suggests: you can put the core logic into a function that returns the output but doesn't handle outputting the value (e.g. to the console). This allows such code to be atomic and testable - congruent with the Single Responsibility Principle. Also, thereturn
statement can eliminate the need forelse
keywords within a function. One drawback is that calling a function on each iteration may slow down operations but should be negligible for a set of numbers 1 through 100.
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.
function fizzBuzz(value) {
if (value % 15 === 0) return "FizzBuzz";
if (value % 3 === 0) return "Fizz";
if (value % 5 === 0) return "Buzz";
return value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
One option I did consider is to minimize the number of modulo operations, append to the output string instead of outputting it. If you are trying to optimize for speed, this might not be the approach to take because appending to the string may be much slower than doing an extra couple modulo operations. Try a comparison in this jsPerf test.
function fizzBuzz(value) {
var output = "";
if (value % 3 === 0) output += "Fizz";
if (value % 5 === 0) output += "Buzz";
return output || value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
3
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
2 days ago
4
@CaseyB I don't think arguing about micro-optimizations like this when OP is a beginner is a good idea at all.
– Voile
yesterday
3
@Voile One million percent. Even if this were a production-ready, enterprise-grade, cloud-based microservice™, readability and maintainability is always the number one concern. The concept of micro-optimizing a JavaScript FizzBuzz is honestly hilarious.
– Michael
yesterday
6
@Michael FizzBuzz Enterprise Edition begs to differ. :P But FWIW, I believe Casey was merely providing a counterargument for the microoptimization that the answer itself first suggested.
– Mateen Ulhaq
yesterday
2
@MateenUlhaq 260 issues... man, they really need to refactor it, arrange UATs, and use Kanban.
– Juha Untinen
yesterday
|
show 5 more comments
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.
Style Guide Consider following a style guide. Many common style guides advise separating keywords with a space - e.g.if (
instead ofif(
.
Use consistent indentation The first and last lines within thefor
loop 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 - If you are going to use block statements then expand them so each statement is on a separate line. Otherwise, just put the single statement without a block. Also use consistent statement termination. The first three conditional blocks don't have a semi-colon after the statement within the block, while the last one (in theelse
block) does.
Consider default valueoutput
is never left as""
because it is always set in one of theif
,else if
orelse
blocks. Theelse
could be eliminated ifoutput
is assigned toi
. In a strongly typed language this might have ramifications but not in JavaScript.
Abstract logic into a function As Paul's answer suggests: you can put the core logic into a function that returns the output but doesn't handle outputting the value (e.g. to the console). This allows such code to be atomic and testable - congruent with the Single Responsibility Principle. Also, thereturn
statement can eliminate the need forelse
keywords within a function. One drawback is that calling a function on each iteration may slow down operations but should be negligible for a set of numbers 1 through 100.
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.
function fizzBuzz(value) {
if (value % 15 === 0) return "FizzBuzz";
if (value % 3 === 0) return "Fizz";
if (value % 5 === 0) return "Buzz";
return value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
One option I did consider is to minimize the number of modulo operations, append to the output string instead of outputting it. If you are trying to optimize for speed, this might not be the approach to take because appending to the string may be much slower than doing an extra couple modulo operations. Try a comparison in this jsPerf test.
function fizzBuzz(value) {
var output = "";
if (value % 3 === 0) output += "Fizz";
if (value % 5 === 0) output += "Buzz";
return output || value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
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.
Style Guide Consider following a style guide. Many common style guides advise separating keywords with a space - e.g.if (
instead ofif(
.
Use consistent indentation The first and last lines within thefor
loop 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 - If you are going to use block statements then expand them so each statement is on a separate line. Otherwise, just put the single statement without a block. Also use consistent statement termination. The first three conditional blocks don't have a semi-colon after the statement within the block, while the last one (in theelse
block) does.
Consider default valueoutput
is never left as""
because it is always set in one of theif
,else if
orelse
blocks. Theelse
could be eliminated ifoutput
is assigned toi
. In a strongly typed language this might have ramifications but not in JavaScript.
Abstract logic into a function As Paul's answer suggests: you can put the core logic into a function that returns the output but doesn't handle outputting the value (e.g. to the console). This allows such code to be atomic and testable - congruent with the Single Responsibility Principle. Also, thereturn
statement can eliminate the need forelse
keywords within a function. One drawback is that calling a function on each iteration may slow down operations but should be negligible for a set of numbers 1 through 100.
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.
function fizzBuzz(value) {
if (value % 15 === 0) return "FizzBuzz";
if (value % 3 === 0) return "Fizz";
if (value % 5 === 0) return "Buzz";
return value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
One option I did consider is to minimize the number of modulo operations, append to the output string instead of outputting it. If you are trying to optimize for speed, this might not be the approach to take because appending to the string may be much slower than doing an extra couple modulo operations. Try a comparison in this jsPerf test.
function fizzBuzz(value) {
var output = "";
if (value % 3 === 0) output += "Fizz";
if (value % 5 === 0) output += "Buzz";
return output || value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
function fizzBuzz(value) {
if (value % 15 === 0) return "FizzBuzz";
if (value % 3 === 0) return "Fizz";
if (value % 5 === 0) return "Buzz";
return value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
function fizzBuzz(value) {
if (value % 15 === 0) return "FizzBuzz";
if (value % 3 === 0) return "Fizz";
if (value % 5 === 0) return "Buzz";
return value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
function fizzBuzz(value) {
var output = "";
if (value % 3 === 0) output += "Fizz";
if (value % 5 === 0) output += "Buzz";
return output || value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
function fizzBuzz(value) {
var output = "";
if (value % 3 === 0) output += "Fizz";
if (value % 5 === 0) output += "Buzz";
return output || value;
}
for (var i = 1; i <= 100; i++) {
console.log(fizzBuzz(i));
}
edited yesterday
answered 2 days ago
Sᴀᴍ OnᴇᴌᴀSᴀᴍ Onᴇᴌᴀ
8,98362157
8,98362157
3
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
2 days ago
4
@CaseyB I don't think arguing about micro-optimizations like this when OP is a beginner is a good idea at all.
– Voile
yesterday
3
@Voile One million percent. Even if this were a production-ready, enterprise-grade, cloud-based microservice™, readability and maintainability is always the number one concern. The concept of micro-optimizing a JavaScript FizzBuzz is honestly hilarious.
– Michael
yesterday
6
@Michael FizzBuzz Enterprise Edition begs to differ. :P But FWIW, I believe Casey was merely providing a counterargument for the microoptimization that the answer itself first suggested.
– Mateen Ulhaq
yesterday
2
@MateenUlhaq 260 issues... man, they really need to refactor it, arrange UATs, and use Kanban.
– Juha Untinen
yesterday
|
show 5 more comments
3
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
2 days ago
4
@CaseyB I don't think arguing about micro-optimizations like this when OP is a beginner is a good idea at all.
– Voile
yesterday
3
@Voile One million percent. Even if this were a production-ready, enterprise-grade, cloud-based microservice™, readability and maintainability is always the number one concern. The concept of micro-optimizing a JavaScript FizzBuzz is honestly hilarious.
– Michael
yesterday
6
@Michael FizzBuzz Enterprise Edition begs to differ. :P But FWIW, I believe Casey was merely providing a counterargument for the microoptimization that the answer itself first suggested.
– Mateen Ulhaq
yesterday
2
@MateenUlhaq 260 issues... man, they really need to refactor it, arrange UATs, and use Kanban.
– Juha Untinen
yesterday
3
3
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
2 days ago
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
2 days ago
4
4
@CaseyB I don't think arguing about micro-optimizations like this when OP is a beginner is a good idea at all.
– Voile
yesterday
@CaseyB I don't think arguing about micro-optimizations like this when OP is a beginner is a good idea at all.
– Voile
yesterday
3
3
@Voile One million percent. Even if this were a production-ready, enterprise-grade, cloud-based microservice™, readability and maintainability is always the number one concern. The concept of micro-optimizing a JavaScript FizzBuzz is honestly hilarious.
– Michael
yesterday
@Voile One million percent. Even if this were a production-ready, enterprise-grade, cloud-based microservice™, readability and maintainability is always the number one concern. The concept of micro-optimizing a JavaScript FizzBuzz is honestly hilarious.
– Michael
yesterday
6
6
@Michael FizzBuzz Enterprise Edition begs to differ. :P But FWIW, I believe Casey was merely providing a counterargument for the microoptimization that the answer itself first suggested.
– Mateen Ulhaq
yesterday
@Michael FizzBuzz Enterprise Edition begs to differ. :P But FWIW, I believe Casey was merely providing a counterargument for the microoptimization that the answer itself first suggested.
– Mateen Ulhaq
yesterday
2
2
@MateenUlhaq 260 issues... man, they really need to refactor it, arrange UATs, and use Kanban.
– Juha Untinen
yesterday
@MateenUlhaq 260 issues... man, they really need to refactor it, arrange UATs, and use Kanban.
– Juha Untinen
yesterday
|
show 5 more comments
One of the problems is that the case where you check i % 15
(i.e. i
is a multiple of 3 and 5) is unnecessary. You have the concept of 3 and 5 repeated, and the concept of Fizz and Buzz repeated.
This is not currently much of a problem but suppose someone asks you to extend your program to print "Jazz" when i
is a multiple of 7. Using your current strategy we now need a case where i
is a multiple of:
- 3
- 5
- 7
- 3 and 5
- 3 and 7
- 5 and 7
- 3 and 5 and 7
It would look something like this:
for(var i = 1;i <= 100; i++) {
var output = "";
if(i % 105 == 0) {output = "FizzBuzzJazz"}
else if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 35 == 0) {output = "BuzzJazz"}
else if(i % 21 == 0) {output = "FizzJazz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else if(i % 7 == 0) {output = "Jazz"}
else { output = i; }
console.log(output);
}
See how quickly that got out of hand? If we add a fourth word it becomes even worse.
If we use a different strategy by appending text to the output
variable, we can get away with having as few conditions as we have words.
for(var i = 1; i <= 100; i++) {
var output = "";
if (i % 3 === 0) {output += "Fizz";}
if (i % 5 === 0) {output += "Buzz";}
if (i % 7 === 0) {output += "Jazz";}
console.log(output === "" ? i : output);
}
(I've fixed a few other things as suggested in Sam's answer)
One thing that might be new to you here, if you're a beginner, is that the expression used as the argument for console.log
or called the conditional or ternary operator. Ours says that if the output is blank (i.e. not a multiple of 3, 5 or 7) then print i
, else print the string that we've compounded.
The ternary operator can always be replaced by an if-statement if you're not yet comfortable with it.
@PatrickRoberts True. I wanted to focus more on the approach to solving the problem than those things. Buuut I did correct everything else (I couldn't help myself) so yeah I've made that change as well. Thanks
– Michael
10 hours ago
1
You forgot the equality in the ternary expression. I tried to edit myself but I can only make edits of at least 6 characters :|
– Patrick Roberts
9 hours ago
add a comment |
One of the problems is that the case where you check i % 15
(i.e. i
is a multiple of 3 and 5) is unnecessary. You have the concept of 3 and 5 repeated, and the concept of Fizz and Buzz repeated.
This is not currently much of a problem but suppose someone asks you to extend your program to print "Jazz" when i
is a multiple of 7. Using your current strategy we now need a case where i
is a multiple of:
- 3
- 5
- 7
- 3 and 5
- 3 and 7
- 5 and 7
- 3 and 5 and 7
It would look something like this:
for(var i = 1;i <= 100; i++) {
var output = "";
if(i % 105 == 0) {output = "FizzBuzzJazz"}
else if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 35 == 0) {output = "BuzzJazz"}
else if(i % 21 == 0) {output = "FizzJazz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else if(i % 7 == 0) {output = "Jazz"}
else { output = i; }
console.log(output);
}
See how quickly that got out of hand? If we add a fourth word it becomes even worse.
If we use a different strategy by appending text to the output
variable, we can get away with having as few conditions as we have words.
for(var i = 1; i <= 100; i++) {
var output = "";
if (i % 3 === 0) {output += "Fizz";}
if (i % 5 === 0) {output += "Buzz";}
if (i % 7 === 0) {output += "Jazz";}
console.log(output === "" ? i : output);
}
(I've fixed a few other things as suggested in Sam's answer)
One thing that might be new to you here, if you're a beginner, is that the expression used as the argument for console.log
or called the conditional or ternary operator. Ours says that if the output is blank (i.e. not a multiple of 3, 5 or 7) then print i
, else print the string that we've compounded.
The ternary operator can always be replaced by an if-statement if you're not yet comfortable with it.
@PatrickRoberts True. I wanted to focus more on the approach to solving the problem than those things. Buuut I did correct everything else (I couldn't help myself) so yeah I've made that change as well. Thanks
– Michael
10 hours ago
1
You forgot the equality in the ternary expression. I tried to edit myself but I can only make edits of at least 6 characters :|
– Patrick Roberts
9 hours ago
add a comment |
One of the problems is that the case where you check i % 15
(i.e. i
is a multiple of 3 and 5) is unnecessary. You have the concept of 3 and 5 repeated, and the concept of Fizz and Buzz repeated.
This is not currently much of a problem but suppose someone asks you to extend your program to print "Jazz" when i
is a multiple of 7. Using your current strategy we now need a case where i
is a multiple of:
- 3
- 5
- 7
- 3 and 5
- 3 and 7
- 5 and 7
- 3 and 5 and 7
It would look something like this:
for(var i = 1;i <= 100; i++) {
var output = "";
if(i % 105 == 0) {output = "FizzBuzzJazz"}
else if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 35 == 0) {output = "BuzzJazz"}
else if(i % 21 == 0) {output = "FizzJazz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else if(i % 7 == 0) {output = "Jazz"}
else { output = i; }
console.log(output);
}
See how quickly that got out of hand? If we add a fourth word it becomes even worse.
If we use a different strategy by appending text to the output
variable, we can get away with having as few conditions as we have words.
for(var i = 1; i <= 100; i++) {
var output = "";
if (i % 3 === 0) {output += "Fizz";}
if (i % 5 === 0) {output += "Buzz";}
if (i % 7 === 0) {output += "Jazz";}
console.log(output === "" ? i : output);
}
(I've fixed a few other things as suggested in Sam's answer)
One thing that might be new to you here, if you're a beginner, is that the expression used as the argument for console.log
or called the conditional or ternary operator. Ours says that if the output is blank (i.e. not a multiple of 3, 5 or 7) then print i
, else print the string that we've compounded.
The ternary operator can always be replaced by an if-statement if you're not yet comfortable with it.
One of the problems is that the case where you check i % 15
(i.e. i
is a multiple of 3 and 5) is unnecessary. You have the concept of 3 and 5 repeated, and the concept of Fizz and Buzz repeated.
This is not currently much of a problem but suppose someone asks you to extend your program to print "Jazz" when i
is a multiple of 7. Using your current strategy we now need a case where i
is a multiple of:
- 3
- 5
- 7
- 3 and 5
- 3 and 7
- 5 and 7
- 3 and 5 and 7
It would look something like this:
for(var i = 1;i <= 100; i++) {
var output = "";
if(i % 105 == 0) {output = "FizzBuzzJazz"}
else if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 35 == 0) {output = "BuzzJazz"}
else if(i % 21 == 0) {output = "FizzJazz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else if(i % 7 == 0) {output = "Jazz"}
else { output = i; }
console.log(output);
}
See how quickly that got out of hand? If we add a fourth word it becomes even worse.
If we use a different strategy by appending text to the output
variable, we can get away with having as few conditions as we have words.
for(var i = 1; i <= 100; i++) {
var output = "";
if (i % 3 === 0) {output += "Fizz";}
if (i % 5 === 0) {output += "Buzz";}
if (i % 7 === 0) {output += "Jazz";}
console.log(output === "" ? i : output);
}
(I've fixed a few other things as suggested in Sam's answer)
One thing that might be new to you here, if you're a beginner, is that the expression used as the argument for console.log
or called the conditional or ternary operator. Ours says that if the output is blank (i.e. not a multiple of 3, 5 or 7) then print i
, else print the string that we've compounded.
The ternary operator can always be replaced by an if-statement if you're not yet comfortable with it.
for(var i = 1;i <= 100; i++) {
var output = "";
if(i % 105 == 0) {output = "FizzBuzzJazz"}
else if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 35 == 0) {output = "BuzzJazz"}
else if(i % 21 == 0) {output = "FizzJazz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else if(i % 7 == 0) {output = "Jazz"}
else { output = i; }
console.log(output);
}
for(var i = 1;i <= 100; i++) {
var output = "";
if(i % 105 == 0) {output = "FizzBuzzJazz"}
else if(i % 15 == 0) {output = "FizzBuzz"}
else if(i % 35 == 0) {output = "BuzzJazz"}
else if(i % 21 == 0) {output = "FizzJazz"}
else if(i % 3 == 0) {output = "Fizz"}
else if(i % 5 == 0) {output = "Buzz"}
else if(i % 7 == 0) {output = "Jazz"}
else { output = i; }
console.log(output);
}
for(var i = 1; i <= 100; i++) {
var output = "";
if (i % 3 === 0) {output += "Fizz";}
if (i % 5 === 0) {output += "Buzz";}
if (i % 7 === 0) {output += "Jazz";}
console.log(output === "" ? i : output);
}
for(var i = 1; i <= 100; i++) {
var output = "";
if (i % 3 === 0) {output += "Fizz";}
if (i % 5 === 0) {output += "Buzz";}
if (i % 7 === 0) {output += "Jazz";}
console.log(output === "" ? i : output);
}
edited 9 hours ago
answered yesterday
MichaelMichael
668310
668310
@PatrickRoberts True. I wanted to focus more on the approach to solving the problem than those things. Buuut I did correct everything else (I couldn't help myself) so yeah I've made that change as well. Thanks
– Michael
10 hours ago
1
You forgot the equality in the ternary expression. I tried to edit myself but I can only make edits of at least 6 characters :|
– Patrick Roberts
9 hours ago
add a comment |
@PatrickRoberts True. I wanted to focus more on the approach to solving the problem than those things. Buuut I did correct everything else (I couldn't help myself) so yeah I've made that change as well. Thanks
– Michael
10 hours ago
1
You forgot the equality in the ternary expression. I tried to edit myself but I can only make edits of at least 6 characters :|
– Patrick Roberts
9 hours ago
@PatrickRoberts True. I wanted to focus more on the approach to solving the problem than those things. Buuut I did correct everything else (I couldn't help myself) so yeah I've made that change as well. Thanks
– Michael
10 hours ago
@PatrickRoberts True. I wanted to focus more on the approach to solving the problem than those things. Buuut I did correct everything else (I couldn't help myself) so yeah I've made that change as well. Thanks
– Michael
10 hours ago
1
1
You forgot the equality in the ternary expression. I tried to edit myself but I can only make edits of at least 6 characters :|
– Patrick Roberts
9 hours ago
You forgot the equality in the ternary expression. I tried to edit myself but I can only make edits of at least 6 characters :|
– Patrick Roberts
9 hours ago
add a comment |
I think it's fine as is, though some folks like to return early instead of using a series of if..else. For example:
function calc(i) {
if(i % 15 == 0) return "FizzBuzz";
if(i % 3 == 0) return "Fizz";
if(i % 5 == 0) return "Buzz";
return i;
}
for(var i=1;i<=100;i++) {
console.log(calc(i));
}
1
"Returning early" is often called "multiple exit points" and is sometimes considered poor practice.
– Moby Disk
yesterday
5
"Returning early" is often called "validation" and is often considered good practice.
– nurettin
14 hours ago
1
Returning early has many names and people get very worked up about whether their way is best.
– Tim B
8 hours ago
add a comment |
I think it's fine as is, though some folks like to return early instead of using a series of if..else. For example:
function calc(i) {
if(i % 15 == 0) return "FizzBuzz";
if(i % 3 == 0) return "Fizz";
if(i % 5 == 0) return "Buzz";
return i;
}
for(var i=1;i<=100;i++) {
console.log(calc(i));
}
1
"Returning early" is often called "multiple exit points" and is sometimes considered poor practice.
– Moby Disk
yesterday
5
"Returning early" is often called "validation" and is often considered good practice.
– nurettin
14 hours ago
1
Returning early has many names and people get very worked up about whether their way is best.
– Tim B
8 hours ago
add a comment |
I think it's fine as is, though some folks like to return early instead of using a series of if..else. For example:
function calc(i) {
if(i % 15 == 0) return "FizzBuzz";
if(i % 3 == 0) return "Fizz";
if(i % 5 == 0) return "Buzz";
return i;
}
for(var i=1;i<=100;i++) {
console.log(calc(i));
}
I think it's fine as is, though some folks like to return early instead of using a series of if..else. For example:
function calc(i) {
if(i % 15 == 0) return "FizzBuzz";
if(i % 3 == 0) return "Fizz";
if(i % 5 == 0) return "Buzz";
return i;
}
for(var i=1;i<=100;i++) {
console.log(calc(i));
}
edited 2 days ago
answered 2 days ago
PaulPaul
31115
31115
1
"Returning early" is often called "multiple exit points" and is sometimes considered poor practice.
– Moby Disk
yesterday
5
"Returning early" is often called "validation" and is often considered good practice.
– nurettin
14 hours ago
1
Returning early has many names and people get very worked up about whether their way is best.
– Tim B
8 hours ago
add a comment |
1
"Returning early" is often called "multiple exit points" and is sometimes considered poor practice.
– Moby Disk
yesterday
5
"Returning early" is often called "validation" and is often considered good practice.
– nurettin
14 hours ago
1
Returning early has many names and people get very worked up about whether their way is best.
– Tim B
8 hours ago
1
1
"Returning early" is often called "multiple exit points" and is sometimes considered poor practice.
– Moby Disk
yesterday
"Returning early" is often called "multiple exit points" and is sometimes considered poor practice.
– Moby Disk
yesterday
5
5
"Returning early" is often called "validation" and is often considered good practice.
– nurettin
14 hours ago
"Returning early" is often called "validation" and is often considered good practice.
– nurettin
14 hours ago
1
1
Returning early has many names and people get very worked up about whether their way is best.
– Tim B
8 hours ago
Returning early has many names and people get very worked up about whether their way is best.
– Tim B
8 hours ago
add a comment |
Expanding on Michael's answer, you could also create an object with all the words you want, with the key being the number your input must be divisible by, to make this more dynamic and a bit more future proof (A lot easier to add a new entry to the object versus adding more lines of code), and the object itself can also be dynamically generated.
divisions = {3: "Fizz", 5: "Buzz", 7: "Jazz"};
for(var i = 1; i <= 100; i++) {
var output = "";
for (var x in divisions) {
if(i % x == 0) output += divisions[x];
}
console.log(output == "" ? i : output);
}
add a comment |
Expanding on Michael's answer, you could also create an object with all the words you want, with the key being the number your input must be divisible by, to make this more dynamic and a bit more future proof (A lot easier to add a new entry to the object versus adding more lines of code), and the object itself can also be dynamically generated.
divisions = {3: "Fizz", 5: "Buzz", 7: "Jazz"};
for(var i = 1; i <= 100; i++) {
var output = "";
for (var x in divisions) {
if(i % x == 0) output += divisions[x];
}
console.log(output == "" ? i : output);
}
add a comment |
Expanding on Michael's answer, you could also create an object with all the words you want, with the key being the number your input must be divisible by, to make this more dynamic and a bit more future proof (A lot easier to add a new entry to the object versus adding more lines of code), and the object itself can also be dynamically generated.
divisions = {3: "Fizz", 5: "Buzz", 7: "Jazz"};
for(var i = 1; i <= 100; i++) {
var output = "";
for (var x in divisions) {
if(i % x == 0) output += divisions[x];
}
console.log(output == "" ? i : output);
}
Expanding on Michael's answer, you could also create an object with all the words you want, with the key being the number your input must be divisible by, to make this more dynamic and a bit more future proof (A lot easier to add a new entry to the object versus adding more lines of code), and the object itself can also be dynamically generated.
divisions = {3: "Fizz", 5: "Buzz", 7: "Jazz"};
for(var i = 1; i <= 100; i++) {
var output = "";
for (var x in divisions) {
if(i % x == 0) output += divisions[x];
}
console.log(output == "" ? i : output);
}
divisions = {3: "Fizz", 5: "Buzz", 7: "Jazz"};
for(var i = 1; i <= 100; i++) {
var output = "";
for (var x in divisions) {
if(i % x == 0) output += divisions[x];
}
console.log(output == "" ? i : output);
}
divisions = {3: "Fizz", 5: "Buzz", 7: "Jazz"};
for(var i = 1; i <= 100; i++) {
var output = "";
for (var x in divisions) {
if(i % x == 0) output += divisions[x];
}
console.log(output == "" ? i : output);
}
edited yesterday
answered yesterday
GrumpyCroutonGrumpyCrouton
241314
241314
add a comment |
add a comment |
From a maintenance stand point, I think it's better that you check for 3 and 5 instead of checking for 15. The issue with checking for 15 is that when you come back to your code 6 months from now, you won't realize that you were just printing fizzbuzz when something was divisible by both 3 and 5. That might matter in the future, but I think that's something worth talking about in an interview.
add a comment |
From a maintenance stand point, I think it's better that you check for 3 and 5 instead of checking for 15. The issue with checking for 15 is that when you come back to your code 6 months from now, you won't realize that you were just printing fizzbuzz when something was divisible by both 3 and 5. That might matter in the future, but I think that's something worth talking about in an interview.
add a comment |
From a maintenance stand point, I think it's better that you check for 3 and 5 instead of checking for 15. The issue with checking for 15 is that when you come back to your code 6 months from now, you won't realize that you were just printing fizzbuzz when something was divisible by both 3 and 5. That might matter in the future, but I think that's something worth talking about in an interview.
From a maintenance stand point, I think it's better that you check for 3 and 5 instead of checking for 15. The issue with checking for 15 is that when you come back to your code 6 months from now, you won't realize that you were just printing fizzbuzz when something was divisible by both 3 and 5. That might matter in the future, but I think that's something worth talking about in an interview.
edited yesterday
answered yesterday
SteveSteve
8510
8510
add a comment |
add a comment |
Though your code is fine, another thing to look is amount of division happening the code, Division is computationally expensive operation and as @Michael has suggested that it is redundant. So always try to minimise the multiplication and division operation in your code.
You have mentioned that you are a beginner programmer. I believe it is a good practice to start familiarising your self with topics like Computational Complexity.
Look here for computational complexity of mathematical functions
New contributor
While I think it's a bit early for a beginning programmer to be learning about things such as memory management (assuming this is one of their FIRST actual programs), I think this is a good answer!
– nostalgk
yesterday
2
This advice screams "premature optimization". Division is not an expensive operation. It's a bit more expensive that addition but it's not really expensive on today's computers. Your mobile phone can do millions of divisions in a fraction of a second. String concatenation is much more expensive, by far. The most important metric in code quality is readability. Also, if you are talking about computational complexity, you should note that your change won't change complexity at all.
– Sulthan
yesterday
The theoretical computational complexity of the underlying division algorithm is practically irrelevant.
– crasic
23 hours ago
@Sulthan The subject computational complexity doesn't take in to account how many operations a machine can perform, it takes into account how many operations/steps are required to solve a problem. While CC of addition i linear, division is quadratic. Dividing only by 3 and 5 would change the complexity and results of those can be saved in a bool variable which can be evaluated in constant time complexity.
– Hemanshu
12 hours ago
1
On a modern CPU integer division takes 1 CPU cycle, the JS version of%
a little longer. JS will use 32bit signed ints when it can so the operation is insignificant in comparison to just calling a function. BTW the linked computational complexity page has nothing to do with how CPUs ALU and FLU process the basic math operations.
– Blindman67
12 hours ago
|
show 4 more comments
Though your code is fine, another thing to look is amount of division happening the code, Division is computationally expensive operation and as @Michael has suggested that it is redundant. So always try to minimise the multiplication and division operation in your code.
You have mentioned that you are a beginner programmer. I believe it is a good practice to start familiarising your self with topics like Computational Complexity.
Look here for computational complexity of mathematical functions
New contributor
While I think it's a bit early for a beginning programmer to be learning about things such as memory management (assuming this is one of their FIRST actual programs), I think this is a good answer!
– nostalgk
yesterday
2
This advice screams "premature optimization". Division is not an expensive operation. It's a bit more expensive that addition but it's not really expensive on today's computers. Your mobile phone can do millions of divisions in a fraction of a second. String concatenation is much more expensive, by far. The most important metric in code quality is readability. Also, if you are talking about computational complexity, you should note that your change won't change complexity at all.
– Sulthan
yesterday
The theoretical computational complexity of the underlying division algorithm is practically irrelevant.
– crasic
23 hours ago
@Sulthan The subject computational complexity doesn't take in to account how many operations a machine can perform, it takes into account how many operations/steps are required to solve a problem. While CC of addition i linear, division is quadratic. Dividing only by 3 and 5 would change the complexity and results of those can be saved in a bool variable which can be evaluated in constant time complexity.
– Hemanshu
12 hours ago
1
On a modern CPU integer division takes 1 CPU cycle, the JS version of%
a little longer. JS will use 32bit signed ints when it can so the operation is insignificant in comparison to just calling a function. BTW the linked computational complexity page has nothing to do with how CPUs ALU and FLU process the basic math operations.
– Blindman67
12 hours ago
|
show 4 more comments
Though your code is fine, another thing to look is amount of division happening the code, Division is computationally expensive operation and as @Michael has suggested that it is redundant. So always try to minimise the multiplication and division operation in your code.
You have mentioned that you are a beginner programmer. I believe it is a good practice to start familiarising your self with topics like Computational Complexity.
Look here for computational complexity of mathematical functions
New contributor
Though your code is fine, another thing to look is amount of division happening the code, Division is computationally expensive operation and as @Michael has suggested that it is redundant. So always try to minimise the multiplication and division operation in your code.
You have mentioned that you are a beginner programmer. I believe it is a good practice to start familiarising your self with topics like Computational Complexity.
Look here for computational complexity of mathematical functions
New contributor
edited 6 hours ago
Sᴀᴍ Onᴇᴌᴀ
8,98362157
8,98362157
New contributor
answered yesterday
HemanshuHemanshu
314
314
New contributor
New contributor
While I think it's a bit early for a beginning programmer to be learning about things such as memory management (assuming this is one of their FIRST actual programs), I think this is a good answer!
– nostalgk
yesterday
2
This advice screams "premature optimization". Division is not an expensive operation. It's a bit more expensive that addition but it's not really expensive on today's computers. Your mobile phone can do millions of divisions in a fraction of a second. String concatenation is much more expensive, by far. The most important metric in code quality is readability. Also, if you are talking about computational complexity, you should note that your change won't change complexity at all.
– Sulthan
yesterday
The theoretical computational complexity of the underlying division algorithm is practically irrelevant.
– crasic
23 hours ago
@Sulthan The subject computational complexity doesn't take in to account how many operations a machine can perform, it takes into account how many operations/steps are required to solve a problem. While CC of addition i linear, division is quadratic. Dividing only by 3 and 5 would change the complexity and results of those can be saved in a bool variable which can be evaluated in constant time complexity.
– Hemanshu
12 hours ago
1
On a modern CPU integer division takes 1 CPU cycle, the JS version of%
a little longer. JS will use 32bit signed ints when it can so the operation is insignificant in comparison to just calling a function. BTW the linked computational complexity page has nothing to do with how CPUs ALU and FLU process the basic math operations.
– Blindman67
12 hours ago
|
show 4 more comments
While I think it's a bit early for a beginning programmer to be learning about things such as memory management (assuming this is one of their FIRST actual programs), I think this is a good answer!
– nostalgk
yesterday
2
This advice screams "premature optimization". Division is not an expensive operation. It's a bit more expensive that addition but it's not really expensive on today's computers. Your mobile phone can do millions of divisions in a fraction of a second. String concatenation is much more expensive, by far. The most important metric in code quality is readability. Also, if you are talking about computational complexity, you should note that your change won't change complexity at all.
– Sulthan
yesterday
The theoretical computational complexity of the underlying division algorithm is practically irrelevant.
– crasic
23 hours ago
@Sulthan The subject computational complexity doesn't take in to account how many operations a machine can perform, it takes into account how many operations/steps are required to solve a problem. While CC of addition i linear, division is quadratic. Dividing only by 3 and 5 would change the complexity and results of those can be saved in a bool variable which can be evaluated in constant time complexity.
– Hemanshu
12 hours ago
1
On a modern CPU integer division takes 1 CPU cycle, the JS version of%
a little longer. JS will use 32bit signed ints when it can so the operation is insignificant in comparison to just calling a function. BTW the linked computational complexity page has nothing to do with how CPUs ALU and FLU process the basic math operations.
– Blindman67
12 hours ago
While I think it's a bit early for a beginning programmer to be learning about things such as memory management (assuming this is one of their FIRST actual programs), I think this is a good answer!
– nostalgk
yesterday
While I think it's a bit early for a beginning programmer to be learning about things such as memory management (assuming this is one of their FIRST actual programs), I think this is a good answer!
– nostalgk
yesterday
2
2
This advice screams "premature optimization". Division is not an expensive operation. It's a bit more expensive that addition but it's not really expensive on today's computers. Your mobile phone can do millions of divisions in a fraction of a second. String concatenation is much more expensive, by far. The most important metric in code quality is readability. Also, if you are talking about computational complexity, you should note that your change won't change complexity at all.
– Sulthan
yesterday
This advice screams "premature optimization". Division is not an expensive operation. It's a bit more expensive that addition but it's not really expensive on today's computers. Your mobile phone can do millions of divisions in a fraction of a second. String concatenation is much more expensive, by far. The most important metric in code quality is readability. Also, if you are talking about computational complexity, you should note that your change won't change complexity at all.
– Sulthan
yesterday
The theoretical computational complexity of the underlying division algorithm is practically irrelevant.
– crasic
23 hours ago
The theoretical computational complexity of the underlying division algorithm is practically irrelevant.
– crasic
23 hours ago
@Sulthan The subject computational complexity doesn't take in to account how many operations a machine can perform, it takes into account how many operations/steps are required to solve a problem. While CC of addition i linear, division is quadratic. Dividing only by 3 and 5 would change the complexity and results of those can be saved in a bool variable which can be evaluated in constant time complexity.
– Hemanshu
12 hours ago
@Sulthan The subject computational complexity doesn't take in to account how many operations a machine can perform, it takes into account how many operations/steps are required to solve a problem. While CC of addition i linear, division is quadratic. Dividing only by 3 and 5 would change the complexity and results of those can be saved in a bool variable which can be evaluated in constant time complexity.
– Hemanshu
12 hours ago
1
1
On a modern CPU integer division takes 1 CPU cycle, the JS version of
%
a little longer. JS will use 32bit signed ints when it can so the operation is insignificant in comparison to just calling a function. BTW the linked computational complexity page has nothing to do with how CPUs ALU and FLU process the basic math operations.– Blindman67
12 hours ago
On a modern CPU integer division takes 1 CPU cycle, the JS version of
%
a little longer. JS will use 32bit signed ints when it can so the operation is insignificant in comparison to just calling a function. BTW the linked computational complexity page has nothing to do with how CPUs ALU and FLU process the basic math operations.– Blindman67
12 hours ago
|
show 4 more comments
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211113%2ffizzbuzz-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
It's hard to give constructive criticism on such a small, simple function. It's a reasonable solution and the only things I could pick out being wrong would be me being extremely picky or a personal preference.
– George
2 days ago
4
You could go with
var output = i;
and then remove that finalelse
block.– user189829
2 days ago
One possible issue is you are relying on a standard fizzbuzz using 3 and 5 (hence checking for 3 * 5 as 15). Someone might give you a test using 2 and 4 to see if you fell into the trap of checking for 8.
– Kickstart
yesterday
It would be better to include the actual spec you are following. The FizzBuzz solution looks fine. Any extra points goes to how you follow the nuance of the spec. If you really wanted to get fancy, you could do it without the explicit "FizzBuzz" part of the algorithm.
– Tombo
yesterday