FizzBuzz: 15 shows Fizz but no Buzz on newline. Why? Just need clarification












1















Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.



Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.



Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use



if ((int%3 == 0) && (int%5 == 0))


either at the end or the beginning of the block is when the changes are visible.



Note:



I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.



My solution:



for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/

else{
console.log(int);
}
}









share|improve this question

























  • Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)

    – CertainPerformance
    Jan 1 at 9:55






  • 1





    if/else if/… are processed in the order you write them. In the quoted code if int$3 == 0 is true the first branch is taken and all the others are skipped. But when int is 15 then int$3 == 0 is true. You need to put the most specific cases first.

    – Richard
    Jan 1 at 9:59











  • @CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.

    – kashfil_aziz
    Jan 1 at 10:02











  • @Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.

    – kashfil_aziz
    Jan 1 at 10:12


















1















Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.



Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.



Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use



if ((int%3 == 0) && (int%5 == 0))


either at the end or the beginning of the block is when the changes are visible.



Note:



I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.



My solution:



for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/

else{
console.log(int);
}
}









share|improve this question

























  • Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)

    – CertainPerformance
    Jan 1 at 9:55






  • 1





    if/else if/… are processed in the order you write them. In the quoted code if int$3 == 0 is true the first branch is taken and all the others are skipped. But when int is 15 then int$3 == 0 is true. You need to put the most specific cases first.

    – Richard
    Jan 1 at 9:59











  • @CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.

    – kashfil_aziz
    Jan 1 at 10:02











  • @Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.

    – kashfil_aziz
    Jan 1 at 10:12
















1












1








1








Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.



Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.



Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use



if ((int%3 == 0) && (int%5 == 0))


either at the end or the beginning of the block is when the changes are visible.



Note:



I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.



My solution:



for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/

else{
console.log(int);
}
}









share|improve this question
















Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.



Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.



Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use



if ((int%3 == 0) && (int%5 == 0))


either at the end or the beginning of the block is when the changes are visible.



Note:



I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.



My solution:



for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/

else{
console.log(int);
}
}






javascript fizzbuzz






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 10:10







kashfil_aziz

















asked Jan 1 at 9:53









kashfil_azizkashfil_aziz

126




126













  • Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)

    – CertainPerformance
    Jan 1 at 9:55






  • 1





    if/else if/… are processed in the order you write them. In the quoted code if int$3 == 0 is true the first branch is taken and all the others are skipped. But when int is 15 then int$3 == 0 is true. You need to put the most specific cases first.

    – Richard
    Jan 1 at 9:59











  • @CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.

    – kashfil_aziz
    Jan 1 at 10:02











  • @Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.

    – kashfil_aziz
    Jan 1 at 10:12





















  • Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)

    – CertainPerformance
    Jan 1 at 9:55






  • 1





    if/else if/… are processed in the order you write them. In the quoted code if int$3 == 0 is true the first branch is taken and all the others are skipped. But when int is 15 then int$3 == 0 is true. You need to put the most specific cases first.

    – Richard
    Jan 1 at 9:59











  • @CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.

    – kashfil_aziz
    Jan 1 at 10:02











  • @Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.

    – kashfil_aziz
    Jan 1 at 10:12



















Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)

– CertainPerformance
Jan 1 at 9:55





Your commented out solution is not inelegant - it's the proper way to do it. (though, you could store the modulo results in variables if you wanted)

– CertainPerformance
Jan 1 at 9:55




1




1





if/else if/… are processed in the order you write them. In the quoted code if int$3 == 0 is true the first branch is taken and all the others are skipped. But when int is 15 then int$3 == 0 is true. You need to put the most specific cases first.

– Richard
Jan 1 at 9:59





if/else if/… are processed in the order you write them. In the quoted code if int$3 == 0 is true the first branch is taken and all the others are skipped. But when int is 15 then int$3 == 0 is true. You need to put the most specific cases first.

– Richard
Jan 1 at 9:59













@CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.

– kashfil_aziz
Jan 1 at 10:02





@CertainPerformance The storing is one of the ideas I had. Just testing out the conventional ways of doing this first.

– kashfil_aziz
Jan 1 at 10:02













@Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.

– kashfil_aziz
Jan 1 at 10:12







@Richard Oh. I didn't know if/else if/.. are processed in the order I write them. Thanks.

– kashfil_aziz
Jan 1 at 10:12














4 Answers
4






active

oldest

votes


















0














In you solution, the following block is dead code :



else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');


This console.log('Fizz'+'Buzz') can never be reached because ((int%3 == 0) && (int%5 == 0)) would mean that (int%3 == 0) and so the first if is executed. Because of the meaning of else if, this later code block is never reached.



So to answer directly :




show their 15 appearing with Fizz but Buzz is on a newline




This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.




my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this.




Because else if blocks order is important, and you chose the wrong one.






share|improve this answer
























  • Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.

    – kashfil_aziz
    Jan 1 at 10:15





















0














If you remove else from else if(int%5 == 0) you will get your desired output I guess.






share|improve this answer































    0














    You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for



    if(int%3 == 0){
    console.log('Fizz');
    }


    And it will never reach the other if statements.






    share|improve this answer































      0














        else if ((int%3 == 0) && (int%5 == 0)){
      console.log('Fizz'+'Buzz');
      }


      Firstly change comparisons into strict equality === it will help you to evade problems in your code;



      Then put it on top of your comparisons, it must look like following code:



      for(let int = 1; int <= 100; int++) {
      if ( i % 3 === 0 && i % 5 === 0 ) {}


      We had to put our most specific match at the top and then filter down our checks to most general.



      You can even wrap it into function so you can call it and reuse;






      share|improve this answer























        Your Answer






        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: "1"
        };
        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: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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%2fstackoverflow.com%2fquestions%2f53994501%2ffizzbuzz-15-shows-fizz-but-no-buzz-on-newline-why-just-need-clarification%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        In you solution, the following block is dead code :



        else if ((int%3 == 0) && (int%5 == 0)){
        console.log('Fizz'+'Buzz');


        This console.log('Fizz'+'Buzz') can never be reached because ((int%3 == 0) && (int%5 == 0)) would mean that (int%3 == 0) and so the first if is executed. Because of the meaning of else if, this later code block is never reached.



        So to answer directly :




        show their 15 appearing with Fizz but Buzz is on a newline




        This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.




        my solution (which is not commented out) only shows Fizz.
        Can anyone explain to me why does it do this.




        Because else if blocks order is important, and you chose the wrong one.






        share|improve this answer
























        • Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.

          – kashfil_aziz
          Jan 1 at 10:15


















        0














        In you solution, the following block is dead code :



        else if ((int%3 == 0) && (int%5 == 0)){
        console.log('Fizz'+'Buzz');


        This console.log('Fizz'+'Buzz') can never be reached because ((int%3 == 0) && (int%5 == 0)) would mean that (int%3 == 0) and so the first if is executed. Because of the meaning of else if, this later code block is never reached.



        So to answer directly :




        show their 15 appearing with Fizz but Buzz is on a newline




        This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.




        my solution (which is not commented out) only shows Fizz.
        Can anyone explain to me why does it do this.




        Because else if blocks order is important, and you chose the wrong one.






        share|improve this answer
























        • Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.

          – kashfil_aziz
          Jan 1 at 10:15
















        0












        0








        0







        In you solution, the following block is dead code :



        else if ((int%3 == 0) && (int%5 == 0)){
        console.log('Fizz'+'Buzz');


        This console.log('Fizz'+'Buzz') can never be reached because ((int%3 == 0) && (int%5 == 0)) would mean that (int%3 == 0) and so the first if is executed. Because of the meaning of else if, this later code block is never reached.



        So to answer directly :




        show their 15 appearing with Fizz but Buzz is on a newline




        This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.




        my solution (which is not commented out) only shows Fizz.
        Can anyone explain to me why does it do this.




        Because else if blocks order is important, and you chose the wrong one.






        share|improve this answer













        In you solution, the following block is dead code :



        else if ((int%3 == 0) && (int%5 == 0)){
        console.log('Fizz'+'Buzz');


        This console.log('Fizz'+'Buzz') can never be reached because ((int%3 == 0) && (int%5 == 0)) would mean that (int%3 == 0) and so the first if is executed. Because of the meaning of else if, this later code block is never reached.



        So to answer directly :




        show their 15 appearing with Fizz but Buzz is on a newline




        This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.




        my solution (which is not commented out) only shows Fizz.
        Can anyone explain to me why does it do this.




        Because else if blocks order is important, and you chose the wrong one.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 10:10









        SuricatSuricat

        1214




        1214













        • Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.

          – kashfil_aziz
          Jan 1 at 10:15





















        • Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.

          – kashfil_aziz
          Jan 1 at 10:15



















        Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.

        – kashfil_aziz
        Jan 1 at 10:15







        Yeah, on a new line is the wrong solution. Just wanted to know why mine doesn't show one in the new line. Thanks for letting me know that block order is important which @Richard also mentioned above in the comments.

        – kashfil_aziz
        Jan 1 at 10:15















        0














        If you remove else from else if(int%5 == 0) you will get your desired output I guess.






        share|improve this answer




























          0














          If you remove else from else if(int%5 == 0) you will get your desired output I guess.






          share|improve this answer


























            0












            0








            0







            If you remove else from else if(int%5 == 0) you will get your desired output I guess.






            share|improve this answer













            If you remove else from else if(int%5 == 0) you will get your desired output I guess.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 1 at 10:00









            Partho63Partho63

            1,8521923




            1,8521923























                0














                You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for



                if(int%3 == 0){
                console.log('Fizz');
                }


                And it will never reach the other if statements.






                share|improve this answer




























                  0














                  You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for



                  if(int%3 == 0){
                  console.log('Fizz');
                  }


                  And it will never reach the other if statements.






                  share|improve this answer


























                    0












                    0








                    0







                    You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for



                    if(int%3 == 0){
                    console.log('Fizz');
                    }


                    And it will never reach the other if statements.






                    share|improve this answer













                    You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for



                    if(int%3 == 0){
                    console.log('Fizz');
                    }


                    And it will never reach the other if statements.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 1 at 10:11









                    KingVKingV

                    373




                    373























                        0














                          else if ((int%3 == 0) && (int%5 == 0)){
                        console.log('Fizz'+'Buzz');
                        }


                        Firstly change comparisons into strict equality === it will help you to evade problems in your code;



                        Then put it on top of your comparisons, it must look like following code:



                        for(let int = 1; int <= 100; int++) {
                        if ( i % 3 === 0 && i % 5 === 0 ) {}


                        We had to put our most specific match at the top and then filter down our checks to most general.



                        You can even wrap it into function so you can call it and reuse;






                        share|improve this answer




























                          0














                            else if ((int%3 == 0) && (int%5 == 0)){
                          console.log('Fizz'+'Buzz');
                          }


                          Firstly change comparisons into strict equality === it will help you to evade problems in your code;



                          Then put it on top of your comparisons, it must look like following code:



                          for(let int = 1; int <= 100; int++) {
                          if ( i % 3 === 0 && i % 5 === 0 ) {}


                          We had to put our most specific match at the top and then filter down our checks to most general.



                          You can even wrap it into function so you can call it and reuse;






                          share|improve this answer


























                            0












                            0








                            0







                              else if ((int%3 == 0) && (int%5 == 0)){
                            console.log('Fizz'+'Buzz');
                            }


                            Firstly change comparisons into strict equality === it will help you to evade problems in your code;



                            Then put it on top of your comparisons, it must look like following code:



                            for(let int = 1; int <= 100; int++) {
                            if ( i % 3 === 0 && i % 5 === 0 ) {}


                            We had to put our most specific match at the top and then filter down our checks to most general.



                            You can even wrap it into function so you can call it and reuse;






                            share|improve this answer













                              else if ((int%3 == 0) && (int%5 == 0)){
                            console.log('Fizz'+'Buzz');
                            }


                            Firstly change comparisons into strict equality === it will help you to evade problems in your code;



                            Then put it on top of your comparisons, it must look like following code:



                            for(let int = 1; int <= 100; int++) {
                            if ( i % 3 === 0 && i % 5 === 0 ) {}


                            We had to put our most specific match at the top and then filter down our checks to most general.



                            You can even wrap it into function so you can call it and reuse;







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 1 at 10:35









                            Max KazionovMax Kazionov

                            11




                            11






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


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




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994501%2ffizzbuzz-15-shows-fizz-but-no-buzz-on-newline-why-just-need-clarification%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

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith