How's my FizzBuzz solution for a beginner?
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 16 hours 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 16 hours ago
This question came from our site for professional and enthusiast programmers.
3
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
15 hours ago
2
You could go withvar output = i;
and then remove that finalelse
block.
– user189829
15 hours ago
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 14 hours ago
Sᴀᴍ Onᴇᴌᴀ
8,60961855
8,60961855
asked 16 hours ago
Zachary WoodsZachary Woods
392
392
migrated from stackoverflow.com 16 hours ago
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com 16 hours ago
This question came from our site for professional and enthusiast programmers.
3
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
15 hours ago
2
You could go withvar output = i;
and then remove that finalelse
block.
– user189829
15 hours ago
add a comment |
3
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
15 hours ago
2
You could go withvar output = i;
and then remove that finalelse
block.
– user189829
15 hours ago
3
3
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
15 hours 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
15 hours ago
2
2
You could go with
var output = i;
and then remove that final else
block.– user189829
15 hours ago
You could go with
var output = i;
and then remove that final else
block.– user189829
15 hours ago
add a comment |
2 Answers
2
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.
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));
}
1
The cost of using string concatenation is much higher than the cost of an extra modulo operation. If you comment out the relatively slow console.log operation, you'll see the first version is significantly faster.
– CaseyB
9 hours ago
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
9 hours ago
1
Regarding style, it is recommended to 1. always add space after a keyword, e.g.if (
. Be consistent withfor (
2. (subjective)else if
could be justif
because we have early returns everywhere. If you removeelse
, you can remove allelse
for the sake of consistency. 3.var
should never be declared insidefor
. This is javascript and vars are hoisted. If you cannot uselet
, declarevar
at the start of the block because that's whatvar
actually means.
– Sulthan
9 hours ago
@Sulthan thanks for the feedback. I have updated my answer. I would suggest you mention the part aboutvar
within afor
loop in an answer of your own - especially if you are working towards the sprortsmanship badge and have upvoted this answer.
– Sᴀᴍ Onᴇᴌᴀ
8 hours ago
1
I disagree with you on the blocks. Each one is a single line; spreading them out doesn't improve readability. It just makes the code longer, which actually hurts readability a tiny bit. The function seems entirely unnecessary, as well.
– jpmc26
8 hours ago
|
show 1 more 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));
}
add a comment |
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%2fhows-my-fizzbuzz-solution-for-a-beginner%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
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.
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));
}
1
The cost of using string concatenation is much higher than the cost of an extra modulo operation. If you comment out the relatively slow console.log operation, you'll see the first version is significantly faster.
– CaseyB
9 hours ago
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
9 hours ago
1
Regarding style, it is recommended to 1. always add space after a keyword, e.g.if (
. Be consistent withfor (
2. (subjective)else if
could be justif
because we have early returns everywhere. If you removeelse
, you can remove allelse
for the sake of consistency. 3.var
should never be declared insidefor
. This is javascript and vars are hoisted. If you cannot uselet
, declarevar
at the start of the block because that's whatvar
actually means.
– Sulthan
9 hours ago
@Sulthan thanks for the feedback. I have updated my answer. I would suggest you mention the part aboutvar
within afor
loop in an answer of your own - especially if you are working towards the sprortsmanship badge and have upvoted this answer.
– Sᴀᴍ Onᴇᴌᴀ
8 hours ago
1
I disagree with you on the blocks. Each one is a single line; spreading them out doesn't improve readability. It just makes the code longer, which actually hurts readability a tiny bit. The function seems entirely unnecessary, as well.
– jpmc26
8 hours ago
|
show 1 more comment
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.
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));
}
1
The cost of using string concatenation is much higher than the cost of an extra modulo operation. If you comment out the relatively slow console.log operation, you'll see the first version is significantly faster.
– CaseyB
9 hours ago
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
9 hours ago
1
Regarding style, it is recommended to 1. always add space after a keyword, e.g.if (
. Be consistent withfor (
2. (subjective)else if
could be justif
because we have early returns everywhere. If you removeelse
, you can remove allelse
for the sake of consistency. 3.var
should never be declared insidefor
. This is javascript and vars are hoisted. If you cannot uselet
, declarevar
at the start of the block because that's whatvar
actually means.
– Sulthan
9 hours ago
@Sulthan thanks for the feedback. I have updated my answer. I would suggest you mention the part aboutvar
within afor
loop in an answer of your own - especially if you are working towards the sprortsmanship badge and have upvoted this answer.
– Sᴀᴍ Onᴇᴌᴀ
8 hours ago
1
I disagree with you on the blocks. Each one is a single line; spreading them out doesn't improve readability. It just makes the code longer, which actually hurts readability a tiny bit. The function seems entirely unnecessary, as well.
– jpmc26
8 hours ago
|
show 1 more comment
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.
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));
}
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.
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));
}
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));
}
edited 7 hours ago
answered 14 hours ago
Sᴀᴍ OnᴇᴌᴀSᴀᴍ Onᴇᴌᴀ
8,60961855
8,60961855
1
The cost of using string concatenation is much higher than the cost of an extra modulo operation. If you comment out the relatively slow console.log operation, you'll see the first version is significantly faster.
– CaseyB
9 hours ago
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
9 hours ago
1
Regarding style, it is recommended to 1. always add space after a keyword, e.g.if (
. Be consistent withfor (
2. (subjective)else if
could be justif
because we have early returns everywhere. If you removeelse
, you can remove allelse
for the sake of consistency. 3.var
should never be declared insidefor
. This is javascript and vars are hoisted. If you cannot uselet
, declarevar
at the start of the block because that's whatvar
actually means.
– Sulthan
9 hours ago
@Sulthan thanks for the feedback. I have updated my answer. I would suggest you mention the part aboutvar
within afor
loop in an answer of your own - especially if you are working towards the sprortsmanship badge and have upvoted this answer.
– Sᴀᴍ Onᴇᴌᴀ
8 hours ago
1
I disagree with you on the blocks. Each one is a single line; spreading them out doesn't improve readability. It just makes the code longer, which actually hurts readability a tiny bit. The function seems entirely unnecessary, as well.
– jpmc26
8 hours ago
|
show 1 more comment
1
The cost of using string concatenation is much higher than the cost of an extra modulo operation. If you comment out the relatively slow console.log operation, you'll see the first version is significantly faster.
– CaseyB
9 hours ago
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
9 hours ago
1
Regarding style, it is recommended to 1. always add space after a keyword, e.g.if (
. Be consistent withfor (
2. (subjective)else if
could be justif
because we have early returns everywhere. If you removeelse
, you can remove allelse
for the sake of consistency. 3.var
should never be declared insidefor
. This is javascript and vars are hoisted. If you cannot uselet
, declarevar
at the start of the block because that's whatvar
actually means.
– Sulthan
9 hours ago
@Sulthan thanks for the feedback. I have updated my answer. I would suggest you mention the part aboutvar
within afor
loop in an answer of your own - especially if you are working towards the sprortsmanship badge and have upvoted this answer.
– Sᴀᴍ Onᴇᴌᴀ
8 hours ago
1
I disagree with you on the blocks. Each one is a single line; spreading them out doesn't improve readability. It just makes the code longer, which actually hurts readability a tiny bit. The function seems entirely unnecessary, as well.
– jpmc26
8 hours ago
1
1
The cost of using string concatenation is much higher than the cost of an extra modulo operation. If you comment out the relatively slow console.log operation, you'll see the first version is significantly faster.
– CaseyB
9 hours ago
The cost of using string concatenation is much higher than the cost of an extra modulo operation. If you comment out the relatively slow console.log operation, you'll see the first version is significantly faster.
– CaseyB
9 hours ago
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
9 hours ago
There is also the "never use semicolons" school. blog.izs.me/2010/12/… feross.org/never-use-semicolons
– Džuris
9 hours ago
1
1
Regarding style, it is recommended to 1. always add space after a keyword, e.g.
if (
. Be consistent with for (
2. (subjective) else if
could be just if
because we have early returns everywhere. If you remove else
, you can remove all else
for the sake of consistency. 3. var
should never be declared inside for
. This is javascript and vars are hoisted. If you cannot use let
, declare var
at the start of the block because that's what var
actually means.– Sulthan
9 hours ago
Regarding style, it is recommended to 1. always add space after a keyword, e.g.
if (
. Be consistent with for (
2. (subjective) else if
could be just if
because we have early returns everywhere. If you remove else
, you can remove all else
for the sake of consistency. 3. var
should never be declared inside for
. This is javascript and vars are hoisted. If you cannot use let
, declare var
at the start of the block because that's what var
actually means.– Sulthan
9 hours ago
@Sulthan thanks for the feedback. I have updated my answer. I would suggest you mention the part about
var
within a for
loop in an answer of your own - especially if you are working towards the sprortsmanship badge and have upvoted this answer.– Sᴀᴍ Onᴇᴌᴀ
8 hours ago
@Sulthan thanks for the feedback. I have updated my answer. I would suggest you mention the part about
var
within a for
loop in an answer of your own - especially if you are working towards the sprortsmanship badge and have upvoted this answer.– Sᴀᴍ Onᴇᴌᴀ
8 hours ago
1
1
I disagree with you on the blocks. Each one is a single line; spreading them out doesn't improve readability. It just makes the code longer, which actually hurts readability a tiny bit. The function seems entirely unnecessary, as well.
– jpmc26
8 hours ago
I disagree with you on the blocks. Each one is a single line; spreading them out doesn't improve readability. It just makes the code longer, which actually hurts readability a tiny bit. The function seems entirely unnecessary, as well.
– jpmc26
8 hours ago
|
show 1 more 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));
}
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));
}
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 15 hours ago
answered 15 hours ago
PaulPaul
29115
29115
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2fhows-my-fizzbuzz-solution-for-a-beginner%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
3
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
15 hours ago
2
You could go with
var output = i;
and then remove that finalelse
block.– user189829
15 hours ago