Why is loop not stopping at defined stop point?









up vote
0
down vote

favorite












I am working on a quiz for an online course, testing understanding of loops. The problem is:



"Write a function called laugh() that takes one parameter, num that represents the number of “ha”s to return. Such that console.log(laugh(3)); prints "hahaha!"" I solved this using the following loop:



function laugh(num) 
var string = ""
for (var i = 0; i < num; i++)
string = string + "ha";

return string + "!";

console.log(laugh(3));


But out of curiosity, I increased num and had the loop return i instead of the string:



function laugh(num) 
var string = "";
for (var i = 0; i < num; i++)
string = string + "ha";

return i;


console.log(laugh(16))


This printed 16. Now I'm confused - shouldn't i only reach 15 since one of the conditions of the loop is that i < num and num is set to 16?










share|improve this question

















  • 1




    The test occurs after the increment.
    – CertainPerformance
    Nov 9 at 3:37










  • no, @Plotisateur, post or pre increment makes absolutely no difference in this code
    – Bravo
    Nov 9 at 3:43










  • @Plotisateur Even if it were a pre-increment (++i), the effect would be the same - the "test" part is checked after the final-expression part is finished.
    – CertainPerformance
    Nov 9 at 3:44










  • Suposse i is equal to 15, then the condition 15 < 16 will be true and for sure you will still be in the loop. So there is no way the method can return 15. When i reach the value 16 then the loop is finished (because 16 < 16 is false) and that value is returned.
    – D. Smania
    Nov 9 at 3:46















up vote
0
down vote

favorite












I am working on a quiz for an online course, testing understanding of loops. The problem is:



"Write a function called laugh() that takes one parameter, num that represents the number of “ha”s to return. Such that console.log(laugh(3)); prints "hahaha!"" I solved this using the following loop:



function laugh(num) 
var string = ""
for (var i = 0; i < num; i++)
string = string + "ha";

return string + "!";

console.log(laugh(3));


But out of curiosity, I increased num and had the loop return i instead of the string:



function laugh(num) 
var string = "";
for (var i = 0; i < num; i++)
string = string + "ha";

return i;


console.log(laugh(16))


This printed 16. Now I'm confused - shouldn't i only reach 15 since one of the conditions of the loop is that i < num and num is set to 16?










share|improve this question

















  • 1




    The test occurs after the increment.
    – CertainPerformance
    Nov 9 at 3:37










  • no, @Plotisateur, post or pre increment makes absolutely no difference in this code
    – Bravo
    Nov 9 at 3:43










  • @Plotisateur Even if it were a pre-increment (++i), the effect would be the same - the "test" part is checked after the final-expression part is finished.
    – CertainPerformance
    Nov 9 at 3:44










  • Suposse i is equal to 15, then the condition 15 < 16 will be true and for sure you will still be in the loop. So there is no way the method can return 15. When i reach the value 16 then the loop is finished (because 16 < 16 is false) and that value is returned.
    – D. Smania
    Nov 9 at 3:46













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am working on a quiz for an online course, testing understanding of loops. The problem is:



"Write a function called laugh() that takes one parameter, num that represents the number of “ha”s to return. Such that console.log(laugh(3)); prints "hahaha!"" I solved this using the following loop:



function laugh(num) 
var string = ""
for (var i = 0; i < num; i++)
string = string + "ha";

return string + "!";

console.log(laugh(3));


But out of curiosity, I increased num and had the loop return i instead of the string:



function laugh(num) 
var string = "";
for (var i = 0; i < num; i++)
string = string + "ha";

return i;


console.log(laugh(16))


This printed 16. Now I'm confused - shouldn't i only reach 15 since one of the conditions of the loop is that i < num and num is set to 16?










share|improve this question













I am working on a quiz for an online course, testing understanding of loops. The problem is:



"Write a function called laugh() that takes one parameter, num that represents the number of “ha”s to return. Such that console.log(laugh(3)); prints "hahaha!"" I solved this using the following loop:



function laugh(num) 
var string = ""
for (var i = 0; i < num; i++)
string = string + "ha";

return string + "!";

console.log(laugh(3));


But out of curiosity, I increased num and had the loop return i instead of the string:



function laugh(num) 
var string = "";
for (var i = 0; i < num; i++)
string = string + "ha";

return i;


console.log(laugh(16))


This printed 16. Now I'm confused - shouldn't i only reach 15 since one of the conditions of the loop is that i < num and num is set to 16?







javascript function loops for-loop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 3:36









Carlo Tapia

212




212







  • 1




    The test occurs after the increment.
    – CertainPerformance
    Nov 9 at 3:37










  • no, @Plotisateur, post or pre increment makes absolutely no difference in this code
    – Bravo
    Nov 9 at 3:43










  • @Plotisateur Even if it were a pre-increment (++i), the effect would be the same - the "test" part is checked after the final-expression part is finished.
    – CertainPerformance
    Nov 9 at 3:44










  • Suposse i is equal to 15, then the condition 15 < 16 will be true and for sure you will still be in the loop. So there is no way the method can return 15. When i reach the value 16 then the loop is finished (because 16 < 16 is false) and that value is returned.
    – D. Smania
    Nov 9 at 3:46













  • 1




    The test occurs after the increment.
    – CertainPerformance
    Nov 9 at 3:37










  • no, @Plotisateur, post or pre increment makes absolutely no difference in this code
    – Bravo
    Nov 9 at 3:43










  • @Plotisateur Even if it were a pre-increment (++i), the effect would be the same - the "test" part is checked after the final-expression part is finished.
    – CertainPerformance
    Nov 9 at 3:44










  • Suposse i is equal to 15, then the condition 15 < 16 will be true and for sure you will still be in the loop. So there is no way the method can return 15. When i reach the value 16 then the loop is finished (because 16 < 16 is false) and that value is returned.
    – D. Smania
    Nov 9 at 3:46








1




1




The test occurs after the increment.
– CertainPerformance
Nov 9 at 3:37




The test occurs after the increment.
– CertainPerformance
Nov 9 at 3:37












no, @Plotisateur, post or pre increment makes absolutely no difference in this code
– Bravo
Nov 9 at 3:43




no, @Plotisateur, post or pre increment makes absolutely no difference in this code
– Bravo
Nov 9 at 3:43












@Plotisateur Even if it were a pre-increment (++i), the effect would be the same - the "test" part is checked after the final-expression part is finished.
– CertainPerformance
Nov 9 at 3:44




@Plotisateur Even if it were a pre-increment (++i), the effect would be the same - the "test" part is checked after the final-expression part is finished.
– CertainPerformance
Nov 9 at 3:44












Suposse i is equal to 15, then the condition 15 < 16 will be true and for sure you will still be in the loop. So there is no way the method can return 15. When i reach the value 16 then the loop is finished (because 16 < 16 is false) and that value is returned.
– D. Smania
Nov 9 at 3:46





Suposse i is equal to 15, then the condition 15 < 16 will be true and for sure you will still be in the loop. So there is no way the method can return 15. When i reach the value 16 then the loop is finished (because 16 < 16 is false) and that value is returned.
– D. Smania
Nov 9 at 3:46













4 Answers
4






active

oldest

votes

















up vote
0
down vote













In for loop first is increment done after that condition is check. So when "i" incremented to 15 then it checks 15<16, true so continue with rest of the code.
After that, "i" incremented to 16 and checks 16<16, false so came out from the loop.



So, If you print "i", then it will print current value of "i" which is 16






share|improve this answer




















  • Thank you. I understand now!
    – Carlo Tapia
    Nov 9 at 18:34

















up vote
0
down vote













The value of i starts at 0, then it is checked to see if it is less than 16, and lastly, it is increased by 1. On the last iteration, i is 15, which satisfies the condition of being less than 15, and then it is incremented. Now, i is 16, which is not less than 16, so the for loop finishes with i equal to 16.






share|improve this answer





























    up vote
    0
    down vote













    See, you start i at 0. Walk through the loop step by step. It will execute for i = 0,1,2,3...15. But now you see, the loop executes for 0. So for any loop of the form (int i = 0; i < num; i++), i reaches num. Convention is to start counting at 0 because that's convention.






    share|improve this answer



























      up vote
      0
      down vote













      The loop exit condition is i < num, so when i >= 16 code string = string + "ha"; is not executing






      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',
        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%2f53219490%2fwhy-is-loop-not-stopping-at-defined-stop-point%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








        up vote
        0
        down vote













        In for loop first is increment done after that condition is check. So when "i" incremented to 15 then it checks 15<16, true so continue with rest of the code.
        After that, "i" incremented to 16 and checks 16<16, false so came out from the loop.



        So, If you print "i", then it will print current value of "i" which is 16






        share|improve this answer




















        • Thank you. I understand now!
          – Carlo Tapia
          Nov 9 at 18:34














        up vote
        0
        down vote













        In for loop first is increment done after that condition is check. So when "i" incremented to 15 then it checks 15<16, true so continue with rest of the code.
        After that, "i" incremented to 16 and checks 16<16, false so came out from the loop.



        So, If you print "i", then it will print current value of "i" which is 16






        share|improve this answer




















        • Thank you. I understand now!
          – Carlo Tapia
          Nov 9 at 18:34












        up vote
        0
        down vote










        up vote
        0
        down vote









        In for loop first is increment done after that condition is check. So when "i" incremented to 15 then it checks 15<16, true so continue with rest of the code.
        After that, "i" incremented to 16 and checks 16<16, false so came out from the loop.



        So, If you print "i", then it will print current value of "i" which is 16






        share|improve this answer












        In for loop first is increment done after that condition is check. So when "i" incremented to 15 then it checks 15<16, true so continue with rest of the code.
        After that, "i" incremented to 16 and checks 16<16, false so came out from the loop.



        So, If you print "i", then it will print current value of "i" which is 16







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 3:45









        Biplab Malakar

        39837




        39837











        • Thank you. I understand now!
          – Carlo Tapia
          Nov 9 at 18:34
















        • Thank you. I understand now!
          – Carlo Tapia
          Nov 9 at 18:34















        Thank you. I understand now!
        – Carlo Tapia
        Nov 9 at 18:34




        Thank you. I understand now!
        – Carlo Tapia
        Nov 9 at 18:34












        up vote
        0
        down vote













        The value of i starts at 0, then it is checked to see if it is less than 16, and lastly, it is increased by 1. On the last iteration, i is 15, which satisfies the condition of being less than 15, and then it is incremented. Now, i is 16, which is not less than 16, so the for loop finishes with i equal to 16.






        share|improve this answer


























          up vote
          0
          down vote













          The value of i starts at 0, then it is checked to see if it is less than 16, and lastly, it is increased by 1. On the last iteration, i is 15, which satisfies the condition of being less than 15, and then it is incremented. Now, i is 16, which is not less than 16, so the for loop finishes with i equal to 16.






          share|improve this answer
























            up vote
            0
            down vote










            up vote
            0
            down vote









            The value of i starts at 0, then it is checked to see if it is less than 16, and lastly, it is increased by 1. On the last iteration, i is 15, which satisfies the condition of being less than 15, and then it is incremented. Now, i is 16, which is not less than 16, so the for loop finishes with i equal to 16.






            share|improve this answer














            The value of i starts at 0, then it is checked to see if it is less than 16, and lastly, it is increased by 1. On the last iteration, i is 15, which satisfies the condition of being less than 15, and then it is incremented. Now, i is 16, which is not less than 16, so the for loop finishes with i equal to 16.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 3:59

























            answered Nov 9 at 3:53









            hev1

            5,4653527




            5,4653527




















                up vote
                0
                down vote













                See, you start i at 0. Walk through the loop step by step. It will execute for i = 0,1,2,3...15. But now you see, the loop executes for 0. So for any loop of the form (int i = 0; i < num; i++), i reaches num. Convention is to start counting at 0 because that's convention.






                share|improve this answer
























                  up vote
                  0
                  down vote













                  See, you start i at 0. Walk through the loop step by step. It will execute for i = 0,1,2,3...15. But now you see, the loop executes for 0. So for any loop of the form (int i = 0; i < num; i++), i reaches num. Convention is to start counting at 0 because that's convention.






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    See, you start i at 0. Walk through the loop step by step. It will execute for i = 0,1,2,3...15. But now you see, the loop executes for 0. So for any loop of the form (int i = 0; i < num; i++), i reaches num. Convention is to start counting at 0 because that's convention.






                    share|improve this answer












                    See, you start i at 0. Walk through the loop step by step. It will execute for i = 0,1,2,3...15. But now you see, the loop executes for 0. So for any loop of the form (int i = 0; i < num; i++), i reaches num. Convention is to start counting at 0 because that's convention.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 9 at 4:00









                    RDJ

                    11




                    11




















                        up vote
                        0
                        down vote













                        The loop exit condition is i < num, so when i >= 16 code string = string + "ha"; is not executing






                        share|improve this answer


























                          up vote
                          0
                          down vote













                          The loop exit condition is i < num, so when i >= 16 code string = string + "ha"; is not executing






                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            The loop exit condition is i < num, so when i >= 16 code string = string + "ha"; is not executing






                            share|improve this answer














                            The loop exit condition is i < num, so when i >= 16 code string = string + "ha"; is not executing







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 9 at 9:45

























                            answered Nov 9 at 3:43









                            Evgeny A. Mamonov

                            39636




                            39636



























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219490%2fwhy-is-loop-not-stopping-at-defined-stop-point%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

                                𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                Crossroads (UK TV series)

                                ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế