FizzBuzz in Javascript












13














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.










share|improve this question















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 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










  • 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
















13














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.










share|improve this question















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 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










  • 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














13












13








13


4





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • 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




    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 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










  • 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










6 Answers
6






active

oldest

votes


















42















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 of if(.


  • Use consistent indentation The first and last lines within the for 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 the else block) does.


  • Consider default value output is never left as "" because it is always set in one of the if, else if or else blocks. The else could be eliminated if output is assigned to i. 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, the return statement can eliminate the need for else 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));
}








share|improve this answer



















  • 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



















22














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:




  1. 3

  2. 5

  3. 7

  4. 3 and 5

  5. 3 and 7

  6. 5 and 7

  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.






share|improve this answer























  • @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



















15














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));
}





share|improve this answer



















  • 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



















3














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);
}








share|improve this answer































    1














    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.






    share|improve this answer































      1














      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






      share|improve this answer










      New contributor




      Hemanshu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.


















      • 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











      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      42















      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 of if(.


      • Use consistent indentation The first and last lines within the for 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 the else block) does.


      • Consider default value output is never left as "" because it is always set in one of the if, else if or else blocks. The else could be eliminated if output is assigned to i. 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, the return statement can eliminate the need for else 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));
      }








      share|improve this answer



















      • 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
















      42















      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 of if(.


      • Use consistent indentation The first and last lines within the for 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 the else block) does.


      • Consider default value output is never left as "" because it is always set in one of the if, else if or else blocks. The else could be eliminated if output is assigned to i. 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, the return statement can eliminate the need for else 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));
      }








      share|improve this answer



















      • 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














      42












      42








      42







      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 of if(.


      • Use consistent indentation The first and last lines within the for 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 the else block) does.


      • Consider default value output is never left as "" because it is always set in one of the if, else if or else blocks. The else could be eliminated if output is assigned to i. 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, the return statement can eliminate the need for else 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));
      }








      share|improve this answer















      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 of if(.


      • Use consistent indentation The first and last lines within the for 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 the else block) does.


      • Consider default value output is never left as "" because it is always set in one of the if, else if or else blocks. The else could be eliminated if output is assigned to i. 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, the return statement can eliminate the need for else 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));
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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














      • 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













      22














      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:




      1. 3

      2. 5

      3. 7

      4. 3 and 5

      5. 3 and 7

      6. 5 and 7

      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.






      share|improve this answer























      • @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
















      22














      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:




      1. 3

      2. 5

      3. 7

      4. 3 and 5

      5. 3 and 7

      6. 5 and 7

      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.






      share|improve this answer























      • @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














      22












      22








      22






      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:




      1. 3

      2. 5

      3. 7

      4. 3 and 5

      5. 3 and 7

      6. 5 and 7

      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.






      share|improve this answer














      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:




      1. 3

      2. 5

      3. 7

      4. 3 and 5

      5. 3 and 7

      6. 5 and 7

      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);
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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


















      • @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











      15














      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));
      }





      share|improve this answer



















      • 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
















      15














      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));
      }





      share|improve this answer



















      • 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














      15












      15








      15






      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));
      }





      share|improve this answer














      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));
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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














      • 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











      3














      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);
      }








      share|improve this answer




























        3














        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);
        }








        share|improve this answer


























          3












          3








          3






          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);
          }








          share|improve this answer














          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);
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          GrumpyCroutonGrumpyCrouton

          241314




          241314























              1














              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.






              share|improve this answer




























                1














                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.






                share|improve this answer


























                  1












                  1








                  1






                  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.






                  share|improve this answer














                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited yesterday

























                  answered yesterday









                  SteveSteve

                  8510




                  8510























                      1














                      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






                      share|improve this answer










                      New contributor




                      Hemanshu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.


















                      • 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
















                      1














                      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






                      share|improve this answer










                      New contributor




                      Hemanshu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.


















                      • 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














                      1












                      1








                      1






                      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






                      share|improve this answer










                      New contributor




                      Hemanshu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      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







                      share|improve this answer










                      New contributor




                      Hemanshu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      share|improve this answer



                      share|improve this answer








                      edited 6 hours ago









                      Sᴀᴍ Onᴇᴌᴀ

                      8,98362157




                      8,98362157






                      New contributor




                      Hemanshu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.









                      answered yesterday









                      HemanshuHemanshu

                      314




                      314




                      New contributor




                      Hemanshu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





                      New contributor





                      Hemanshu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      Hemanshu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.












                      • 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






                      • 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


















                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      An IMO inspired problem

                      Management

                      Has there ever been an instance of an active nuclear power plant within or near a war zone?