“99 bottle of beer on the wall” code not working pure js. if statement and while loop





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Hi I have made a 99 bottles of beer song but my code does not seem to be working im not to sure and I cannot figure it out. I really need some help to see where I am going wrong or some one to show my where I am going wrong. I want to if statements or if there is a different way to do it but I need the set-up of how it is im not to sure how to fix it please help.
Thanks in advance.



This is my JavaScript.



var bottles = 99
bottle = "bottles";
text = "";

var output = document.getElementById('output');
while (bottles > 0) {

if (bottles == 1) {
bottle = "bottle";
text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>";
bottles--;

output.innerHTML += text;
}

if (bottles == 0) {
bottles = "no more";
text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>"

output.innerHTML += text;
}

output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


This is my HTML.



<title>My Title</title>
<script src="javascript.js"></script>
<div style="text-align: center;">
<h1>99 Bottles of Beer Song</h1>

<div id="output"></div>


I also have a fiddle.
http://jsfiddle.net/Matt1990/1wg16qr5/46/










share|improve this question

























  • Can you provide a jsfiddle?

    – 1387233
    Sep 6 '14 at 9:45











  • i just did sorry

    – Matthew
    Sep 6 '14 at 9:46











  • Where to start?..... There is no event starting your app. Syntax errors in your variable declaration ...

    – 1387233
    Sep 6 '14 at 9:49











  • how can i fix them

    – Matthew
    Sep 6 '14 at 9:51


















0















Hi I have made a 99 bottles of beer song but my code does not seem to be working im not to sure and I cannot figure it out. I really need some help to see where I am going wrong or some one to show my where I am going wrong. I want to if statements or if there is a different way to do it but I need the set-up of how it is im not to sure how to fix it please help.
Thanks in advance.



This is my JavaScript.



var bottles = 99
bottle = "bottles";
text = "";

var output = document.getElementById('output');
while (bottles > 0) {

if (bottles == 1) {
bottle = "bottle";
text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>";
bottles--;

output.innerHTML += text;
}

if (bottles == 0) {
bottles = "no more";
text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>"

output.innerHTML += text;
}

output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


This is my HTML.



<title>My Title</title>
<script src="javascript.js"></script>
<div style="text-align: center;">
<h1>99 Bottles of Beer Song</h1>

<div id="output"></div>


I also have a fiddle.
http://jsfiddle.net/Matt1990/1wg16qr5/46/










share|improve this question

























  • Can you provide a jsfiddle?

    – 1387233
    Sep 6 '14 at 9:45











  • i just did sorry

    – Matthew
    Sep 6 '14 at 9:46











  • Where to start?..... There is no event starting your app. Syntax errors in your variable declaration ...

    – 1387233
    Sep 6 '14 at 9:49











  • how can i fix them

    – Matthew
    Sep 6 '14 at 9:51














0












0








0








Hi I have made a 99 bottles of beer song but my code does not seem to be working im not to sure and I cannot figure it out. I really need some help to see where I am going wrong or some one to show my where I am going wrong. I want to if statements or if there is a different way to do it but I need the set-up of how it is im not to sure how to fix it please help.
Thanks in advance.



This is my JavaScript.



var bottles = 99
bottle = "bottles";
text = "";

var output = document.getElementById('output');
while (bottles > 0) {

if (bottles == 1) {
bottle = "bottle";
text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>";
bottles--;

output.innerHTML += text;
}

if (bottles == 0) {
bottles = "no more";
text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>"

output.innerHTML += text;
}

output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


This is my HTML.



<title>My Title</title>
<script src="javascript.js"></script>
<div style="text-align: center;">
<h1>99 Bottles of Beer Song</h1>

<div id="output"></div>


I also have a fiddle.
http://jsfiddle.net/Matt1990/1wg16qr5/46/










share|improve this question
















Hi I have made a 99 bottles of beer song but my code does not seem to be working im not to sure and I cannot figure it out. I really need some help to see where I am going wrong or some one to show my where I am going wrong. I want to if statements or if there is a different way to do it but I need the set-up of how it is im not to sure how to fix it please help.
Thanks in advance.



This is my JavaScript.



var bottles = 99
bottle = "bottles";
text = "";

var output = document.getElementById('output');
while (bottles > 0) {

if (bottles == 1) {
bottle = "bottle";
text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>";
bottles--;

output.innerHTML += text;
}

if (bottles == 0) {
bottles = "no more";
text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>"

output.innerHTML += text;
}

output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


This is my HTML.



<title>My Title</title>
<script src="javascript.js"></script>
<div style="text-align: center;">
<h1>99 Bottles of Beer Song</h1>

<div id="output"></div>


I also have a fiddle.
http://jsfiddle.net/Matt1990/1wg16qr5/46/







javascript if-statement while-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 17 '17 at 13:42









Cerbrus

50.2k1296117




50.2k1296117










asked Sep 6 '14 at 9:44









MatthewMatthew

2611




2611













  • Can you provide a jsfiddle?

    – 1387233
    Sep 6 '14 at 9:45











  • i just did sorry

    – Matthew
    Sep 6 '14 at 9:46











  • Where to start?..... There is no event starting your app. Syntax errors in your variable declaration ...

    – 1387233
    Sep 6 '14 at 9:49











  • how can i fix them

    – Matthew
    Sep 6 '14 at 9:51



















  • Can you provide a jsfiddle?

    – 1387233
    Sep 6 '14 at 9:45











  • i just did sorry

    – Matthew
    Sep 6 '14 at 9:46











  • Where to start?..... There is no event starting your app. Syntax errors in your variable declaration ...

    – 1387233
    Sep 6 '14 at 9:49











  • how can i fix them

    – Matthew
    Sep 6 '14 at 9:51

















Can you provide a jsfiddle?

– 1387233
Sep 6 '14 at 9:45





Can you provide a jsfiddle?

– 1387233
Sep 6 '14 at 9:45













i just did sorry

– Matthew
Sep 6 '14 at 9:46





i just did sorry

– Matthew
Sep 6 '14 at 9:46













Where to start?..... There is no event starting your app. Syntax errors in your variable declaration ...

– 1387233
Sep 6 '14 at 9:49





Where to start?..... There is no event starting your app. Syntax errors in your variable declaration ...

– 1387233
Sep 6 '14 at 9:49













how can i fix them

– Matthew
Sep 6 '14 at 9:51





how can i fix them

– Matthew
Sep 6 '14 at 9:51












5 Answers
5






active

oldest

votes


















2














Your code is full of errors.



To debug client-side code like JavaScript you may use the developer tools in Chrome/Firefox by pressing CTRL + SHIFT + i. It shows syntax errors like the one in the variable declaration part.



For further reading see the Chrome DevTools Overview. Chrome Dev tools are by far better than Firefox/IE/whatever (IMHO)



Chrome Developer tools





Here is the working code. Please compare by yourself.




var bottles = 99,
bottle = "bottles",
text = "",
output = document.getElementById('output');
while (bottles > 0) {
if (bottles == 1) {
bottle = "bottle";
}

text += bottles + " ";
text += bottle + " of beer on the wall, ";
text += bottles + " " + bottle + " of beer.<br>";

bottles--;
text += "Take one down and pass it around, ";
text += + bottles + " bottles of beer on the wall.<hr>"

if (bottles == 0) {
bottles = "no more";
}
output.innerHTML += text;
text = '';
}

output.innerHTML += " No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.";

<div style="text-align: center;">
<h1>99 Bottles of Beer Song</h1>

<div id="output"></div>











share|improve this answer

































    1














    There are syntax errors - and logical errors:



    var bottles = 99
    bottle = "bottles";
    text = "";

    var output = document.getElementById('output');
    while (bottles >= 0) {

    if (bottles == 1) {
    bottle = "bottle";
    }

    // for all > 0
    text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles +
    " " + bottle + " of beer. <br/>";

    bottles--;

    if (bottles == 0) {
    bottles = "no more";
    text += "Take one down and pass it around, " +
    bottles + " bottles of beer on the wall. </p>"; // ; missing
    } // } missing

    output.innerHTML += text; // needs to be done always!

    }

    output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


    Fiddle here






    share|improve this answer





















    • 2





      bottles--; needs to be moved out of the if block

      – Cubed Eye
      Sep 6 '14 at 9:52











    • @CubedEye missed that one, thanks.

      – Axel Amthor
      Sep 6 '14 at 9:54











    • bottle is being defined after it's used. Move if (bottles == 1){...} before text = ...

      – idmean
      Sep 6 '14 at 9:55













    • @wumm No. Its define before the loop.

      – Axel Amthor
      Sep 6 '14 at 9:57











    • @AxelAmthor Yes, that's correct. But what's the purpose of that if this way? The variable will never be used again after it's set in this if. The if executes when bottles is 1. After the if bottles is decremented to 0. The while loop is no longer executed the variable isn't getting used.

      – idmean
      Sep 6 '14 at 9:58





















    1














    Starting Condition




    • We start with 99 bottles

    • Since both 99 and 98 are plural, both this round and next round are set to plural


    Iteration



    Everytime we start a new round, we need to check if the current (and next) num is plural or singular



    Stop Cases



    The while loop will run until num = 0. We have two special cases:





    • num === 1: this round is singular, next round should be no bottles of juice


    • num === 0: this round is no bottles of juice, next round should finish the song with Go to the store and buy some more, 99 bottles of juice on the wall. like the original song.





    singBottles(15, 'juice');

    function singBottles(givenNum, drinkType) {
    let num = givenNum;

    while (num >= 0) {
    const bottlesFirstLine = num === 0 ?
    `no more bottles` :
    `${ num } ${ getPluralState(num) }`;

    const bottlesSecondLine = num - 1 > 0 ?
    `${ num - 1 } ${ getPluralState(num - 1) }` :
    `no more bottles`;

    const firstLine = `${ bottlesFirstLine } of ${ drinkType } on the wall! ${ bottlesFirstLine } of ${ drinkType }!`;

    const secondLine = num - 1 >= 0 ? `Take one down, pass it around... ${ bottlesSecondLine } of ${ drinkType } on the wall!` :
    `Go to the store and buy some more, ${ givenNum } ${ getPluralState(givenNum) } of ${ drinkType } on the wall.`;

    console.log(`${ firstLine }n${ secondLine }n`);

    num--;
    }
    }

    function getPluralState(num) {
    return num === 1 ? 'bottle' : 'bottles';
    }








    share|improve this answer

































      0














      You missed some parenthesis



      var bottles = 99; //You need to declare each var like this var x = "x"; var y = "y"
      var bottle = "bottles"; // or you can declare it like this var x = "x", y ="y"
      var text = "";

      var output = document.getElementById('output');
      while (bottles > 0) {

      if (bottles == 1) {
      bottle = "bottle";
      }//Missed Parenthesis
      text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>";




      if (bottles == 0) {
      bottles = "no more";
      text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>"
      }
      bottles--;//The decreasment is always at the end
      output.innerHTML += text;
      }//Missed parenthesis

      output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


      http://jsfiddle.net/1wg16qr5/49/






      share|improve this answer































        0














        var num = 99;
        var bottles = " bottles";
        var bottlesWillChange = " bottles"

        while (num > 0) {

        if (num === 2) { bottlesWillChange = " bottle"; }
        if (num === 1) { bottles = " bottle"; bottlesWillChange = " bottles"; }

        console.log(num + bottles + " of juice on the wall! " + num + bottles + " of juice! Take one down, pass it around... " + (num - 1) + bottlesWillChange +" of juice on the wall!")

        num--
        }


        Explanation: First we create 3 variables: num, bottles, bottlesWillChange. In the second step we use a while loop with condition num > 0. Main part: if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWillChange variable value to "bottles".






        share|improve this answer


























        • Please explain the code(how it works).

          – Xcoder
          Nov 24 '18 at 23:58











        • @Xcoder, look, first we create 3 variables:num, bottles, bottlesWillChange. In the second step we declarate while loop with condition num > 0. Main part, if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWiillchange variable value to "bottles". I hope it's clear. when whe need change var

          – Andrey Tsapko
          Nov 26 '18 at 14:23












        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%2f25698940%2f99-bottle-of-beer-on-the-wall-code-not-working-pure-js-if-statement-and-while%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        Your code is full of errors.



        To debug client-side code like JavaScript you may use the developer tools in Chrome/Firefox by pressing CTRL + SHIFT + i. It shows syntax errors like the one in the variable declaration part.



        For further reading see the Chrome DevTools Overview. Chrome Dev tools are by far better than Firefox/IE/whatever (IMHO)



        Chrome Developer tools





        Here is the working code. Please compare by yourself.




        var bottles = 99,
        bottle = "bottles",
        text = "",
        output = document.getElementById('output');
        while (bottles > 0) {
        if (bottles == 1) {
        bottle = "bottle";
        }

        text += bottles + " ";
        text += bottle + " of beer on the wall, ";
        text += bottles + " " + bottle + " of beer.<br>";

        bottles--;
        text += "Take one down and pass it around, ";
        text += + bottles + " bottles of beer on the wall.<hr>"

        if (bottles == 0) {
        bottles = "no more";
        }
        output.innerHTML += text;
        text = '';
        }

        output.innerHTML += " No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.";

        <div style="text-align: center;">
        <h1>99 Bottles of Beer Song</h1>

        <div id="output"></div>











        share|improve this answer






























          2














          Your code is full of errors.



          To debug client-side code like JavaScript you may use the developer tools in Chrome/Firefox by pressing CTRL + SHIFT + i. It shows syntax errors like the one in the variable declaration part.



          For further reading see the Chrome DevTools Overview. Chrome Dev tools are by far better than Firefox/IE/whatever (IMHO)



          Chrome Developer tools





          Here is the working code. Please compare by yourself.




          var bottles = 99,
          bottle = "bottles",
          text = "",
          output = document.getElementById('output');
          while (bottles > 0) {
          if (bottles == 1) {
          bottle = "bottle";
          }

          text += bottles + " ";
          text += bottle + " of beer on the wall, ";
          text += bottles + " " + bottle + " of beer.<br>";

          bottles--;
          text += "Take one down and pass it around, ";
          text += + bottles + " bottles of beer on the wall.<hr>"

          if (bottles == 0) {
          bottles = "no more";
          }
          output.innerHTML += text;
          text = '';
          }

          output.innerHTML += " No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.";

          <div style="text-align: center;">
          <h1>99 Bottles of Beer Song</h1>

          <div id="output"></div>











          share|improve this answer




























            2












            2








            2







            Your code is full of errors.



            To debug client-side code like JavaScript you may use the developer tools in Chrome/Firefox by pressing CTRL + SHIFT + i. It shows syntax errors like the one in the variable declaration part.



            For further reading see the Chrome DevTools Overview. Chrome Dev tools are by far better than Firefox/IE/whatever (IMHO)



            Chrome Developer tools





            Here is the working code. Please compare by yourself.




            var bottles = 99,
            bottle = "bottles",
            text = "",
            output = document.getElementById('output');
            while (bottles > 0) {
            if (bottles == 1) {
            bottle = "bottle";
            }

            text += bottles + " ";
            text += bottle + " of beer on the wall, ";
            text += bottles + " " + bottle + " of beer.<br>";

            bottles--;
            text += "Take one down and pass it around, ";
            text += + bottles + " bottles of beer on the wall.<hr>"

            if (bottles == 0) {
            bottles = "no more";
            }
            output.innerHTML += text;
            text = '';
            }

            output.innerHTML += " No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.";

            <div style="text-align: center;">
            <h1>99 Bottles of Beer Song</h1>

            <div id="output"></div>











            share|improve this answer















            Your code is full of errors.



            To debug client-side code like JavaScript you may use the developer tools in Chrome/Firefox by pressing CTRL + SHIFT + i. It shows syntax errors like the one in the variable declaration part.



            For further reading see the Chrome DevTools Overview. Chrome Dev tools are by far better than Firefox/IE/whatever (IMHO)



            Chrome Developer tools





            Here is the working code. Please compare by yourself.




            var bottles = 99,
            bottle = "bottles",
            text = "",
            output = document.getElementById('output');
            while (bottles > 0) {
            if (bottles == 1) {
            bottle = "bottle";
            }

            text += bottles + " ";
            text += bottle + " of beer on the wall, ";
            text += bottles + " " + bottle + " of beer.<br>";

            bottles--;
            text += "Take one down and pass it around, ";
            text += + bottles + " bottles of beer on the wall.<hr>"

            if (bottles == 0) {
            bottles = "no more";
            }
            output.innerHTML += text;
            text = '';
            }

            output.innerHTML += " No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.";

            <div style="text-align: center;">
            <h1>99 Bottles of Beer Song</h1>

            <div id="output"></div>











            var bottles = 99,
            bottle = "bottles",
            text = "",
            output = document.getElementById('output');
            while (bottles > 0) {
            if (bottles == 1) {
            bottle = "bottle";
            }

            text += bottles + " ";
            text += bottle + " of beer on the wall, ";
            text += bottles + " " + bottle + " of beer.<br>";

            bottles--;
            text += "Take one down and pass it around, ";
            text += + bottles + " bottles of beer on the wall.<hr>"

            if (bottles == 0) {
            bottles = "no more";
            }
            output.innerHTML += text;
            text = '';
            }

            output.innerHTML += " No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.";

            <div style="text-align: center;">
            <h1>99 Bottles of Beer Song</h1>

            <div id="output"></div>





            var bottles = 99,
            bottle = "bottles",
            text = "",
            output = document.getElementById('output');
            while (bottles > 0) {
            if (bottles == 1) {
            bottle = "bottle";
            }

            text += bottles + " ";
            text += bottle + " of beer on the wall, ";
            text += bottles + " " + bottle + " of beer.<br>";

            bottles--;
            text += "Take one down and pass it around, ";
            text += + bottles + " bottles of beer on the wall.<hr>"

            if (bottles == 0) {
            bottles = "no more";
            }
            output.innerHTML += text;
            text = '';
            }

            output.innerHTML += " No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.";

            <div style="text-align: center;">
            <h1>99 Bottles of Beer Song</h1>

            <div id="output"></div>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 30 '16 at 16:26

























            answered Sep 6 '14 at 10:00









            13872331387233

            3,66383456




            3,66383456

























                1














                There are syntax errors - and logical errors:



                var bottles = 99
                bottle = "bottles";
                text = "";

                var output = document.getElementById('output');
                while (bottles >= 0) {

                if (bottles == 1) {
                bottle = "bottle";
                }

                // for all > 0
                text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles +
                " " + bottle + " of beer. <br/>";

                bottles--;

                if (bottles == 0) {
                bottles = "no more";
                text += "Take one down and pass it around, " +
                bottles + " bottles of beer on the wall. </p>"; // ; missing
                } // } missing

                output.innerHTML += text; // needs to be done always!

                }

                output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


                Fiddle here






                share|improve this answer





















                • 2





                  bottles--; needs to be moved out of the if block

                  – Cubed Eye
                  Sep 6 '14 at 9:52











                • @CubedEye missed that one, thanks.

                  – Axel Amthor
                  Sep 6 '14 at 9:54











                • bottle is being defined after it's used. Move if (bottles == 1){...} before text = ...

                  – idmean
                  Sep 6 '14 at 9:55













                • @wumm No. Its define before the loop.

                  – Axel Amthor
                  Sep 6 '14 at 9:57











                • @AxelAmthor Yes, that's correct. But what's the purpose of that if this way? The variable will never be used again after it's set in this if. The if executes when bottles is 1. After the if bottles is decremented to 0. The while loop is no longer executed the variable isn't getting used.

                  – idmean
                  Sep 6 '14 at 9:58


















                1














                There are syntax errors - and logical errors:



                var bottles = 99
                bottle = "bottles";
                text = "";

                var output = document.getElementById('output');
                while (bottles >= 0) {

                if (bottles == 1) {
                bottle = "bottle";
                }

                // for all > 0
                text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles +
                " " + bottle + " of beer. <br/>";

                bottles--;

                if (bottles == 0) {
                bottles = "no more";
                text += "Take one down and pass it around, " +
                bottles + " bottles of beer on the wall. </p>"; // ; missing
                } // } missing

                output.innerHTML += text; // needs to be done always!

                }

                output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


                Fiddle here






                share|improve this answer





















                • 2





                  bottles--; needs to be moved out of the if block

                  – Cubed Eye
                  Sep 6 '14 at 9:52











                • @CubedEye missed that one, thanks.

                  – Axel Amthor
                  Sep 6 '14 at 9:54











                • bottle is being defined after it's used. Move if (bottles == 1){...} before text = ...

                  – idmean
                  Sep 6 '14 at 9:55













                • @wumm No. Its define before the loop.

                  – Axel Amthor
                  Sep 6 '14 at 9:57











                • @AxelAmthor Yes, that's correct. But what's the purpose of that if this way? The variable will never be used again after it's set in this if. The if executes when bottles is 1. After the if bottles is decremented to 0. The while loop is no longer executed the variable isn't getting used.

                  – idmean
                  Sep 6 '14 at 9:58
















                1












                1








                1







                There are syntax errors - and logical errors:



                var bottles = 99
                bottle = "bottles";
                text = "";

                var output = document.getElementById('output');
                while (bottles >= 0) {

                if (bottles == 1) {
                bottle = "bottle";
                }

                // for all > 0
                text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles +
                " " + bottle + " of beer. <br/>";

                bottles--;

                if (bottles == 0) {
                bottles = "no more";
                text += "Take one down and pass it around, " +
                bottles + " bottles of beer on the wall. </p>"; // ; missing
                } // } missing

                output.innerHTML += text; // needs to be done always!

                }

                output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


                Fiddle here






                share|improve this answer















                There are syntax errors - and logical errors:



                var bottles = 99
                bottle = "bottles";
                text = "";

                var output = document.getElementById('output');
                while (bottles >= 0) {

                if (bottles == 1) {
                bottle = "bottle";
                }

                // for all > 0
                text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles +
                " " + bottle + " of beer. <br/>";

                bottles--;

                if (bottles == 0) {
                bottles = "no more";
                text += "Take one down and pass it around, " +
                bottles + " bottles of beer on the wall. </p>"; // ; missing
                } // } missing

                output.innerHTML += text; // needs to be done always!

                }

                output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


                Fiddle here







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 6 '14 at 10:03

























                answered Sep 6 '14 at 9:49









                Axel AmthorAxel Amthor

                9,71411338




                9,71411338








                • 2





                  bottles--; needs to be moved out of the if block

                  – Cubed Eye
                  Sep 6 '14 at 9:52











                • @CubedEye missed that one, thanks.

                  – Axel Amthor
                  Sep 6 '14 at 9:54











                • bottle is being defined after it's used. Move if (bottles == 1){...} before text = ...

                  – idmean
                  Sep 6 '14 at 9:55













                • @wumm No. Its define before the loop.

                  – Axel Amthor
                  Sep 6 '14 at 9:57











                • @AxelAmthor Yes, that's correct. But what's the purpose of that if this way? The variable will never be used again after it's set in this if. The if executes when bottles is 1. After the if bottles is decremented to 0. The while loop is no longer executed the variable isn't getting used.

                  – idmean
                  Sep 6 '14 at 9:58
















                • 2





                  bottles--; needs to be moved out of the if block

                  – Cubed Eye
                  Sep 6 '14 at 9:52











                • @CubedEye missed that one, thanks.

                  – Axel Amthor
                  Sep 6 '14 at 9:54











                • bottle is being defined after it's used. Move if (bottles == 1){...} before text = ...

                  – idmean
                  Sep 6 '14 at 9:55













                • @wumm No. Its define before the loop.

                  – Axel Amthor
                  Sep 6 '14 at 9:57











                • @AxelAmthor Yes, that's correct. But what's the purpose of that if this way? The variable will never be used again after it's set in this if. The if executes when bottles is 1. After the if bottles is decremented to 0. The while loop is no longer executed the variable isn't getting used.

                  – idmean
                  Sep 6 '14 at 9:58










                2




                2





                bottles--; needs to be moved out of the if block

                – Cubed Eye
                Sep 6 '14 at 9:52





                bottles--; needs to be moved out of the if block

                – Cubed Eye
                Sep 6 '14 at 9:52













                @CubedEye missed that one, thanks.

                – Axel Amthor
                Sep 6 '14 at 9:54





                @CubedEye missed that one, thanks.

                – Axel Amthor
                Sep 6 '14 at 9:54













                bottle is being defined after it's used. Move if (bottles == 1){...} before text = ...

                – idmean
                Sep 6 '14 at 9:55







                bottle is being defined after it's used. Move if (bottles == 1){...} before text = ...

                – idmean
                Sep 6 '14 at 9:55















                @wumm No. Its define before the loop.

                – Axel Amthor
                Sep 6 '14 at 9:57





                @wumm No. Its define before the loop.

                – Axel Amthor
                Sep 6 '14 at 9:57













                @AxelAmthor Yes, that's correct. But what's the purpose of that if this way? The variable will never be used again after it's set in this if. The if executes when bottles is 1. After the if bottles is decremented to 0. The while loop is no longer executed the variable isn't getting used.

                – idmean
                Sep 6 '14 at 9:58







                @AxelAmthor Yes, that's correct. But what's the purpose of that if this way? The variable will never be used again after it's set in this if. The if executes when bottles is 1. After the if bottles is decremented to 0. The while loop is no longer executed the variable isn't getting used.

                – idmean
                Sep 6 '14 at 9:58













                1














                Starting Condition




                • We start with 99 bottles

                • Since both 99 and 98 are plural, both this round and next round are set to plural


                Iteration



                Everytime we start a new round, we need to check if the current (and next) num is plural or singular



                Stop Cases



                The while loop will run until num = 0. We have two special cases:





                • num === 1: this round is singular, next round should be no bottles of juice


                • num === 0: this round is no bottles of juice, next round should finish the song with Go to the store and buy some more, 99 bottles of juice on the wall. like the original song.





                singBottles(15, 'juice');

                function singBottles(givenNum, drinkType) {
                let num = givenNum;

                while (num >= 0) {
                const bottlesFirstLine = num === 0 ?
                `no more bottles` :
                `${ num } ${ getPluralState(num) }`;

                const bottlesSecondLine = num - 1 > 0 ?
                `${ num - 1 } ${ getPluralState(num - 1) }` :
                `no more bottles`;

                const firstLine = `${ bottlesFirstLine } of ${ drinkType } on the wall! ${ bottlesFirstLine } of ${ drinkType }!`;

                const secondLine = num - 1 >= 0 ? `Take one down, pass it around... ${ bottlesSecondLine } of ${ drinkType } on the wall!` :
                `Go to the store and buy some more, ${ givenNum } ${ getPluralState(givenNum) } of ${ drinkType } on the wall.`;

                console.log(`${ firstLine }n${ secondLine }n`);

                num--;
                }
                }

                function getPluralState(num) {
                return num === 1 ? 'bottle' : 'bottles';
                }








                share|improve this answer






























                  1














                  Starting Condition




                  • We start with 99 bottles

                  • Since both 99 and 98 are plural, both this round and next round are set to plural


                  Iteration



                  Everytime we start a new round, we need to check if the current (and next) num is plural or singular



                  Stop Cases



                  The while loop will run until num = 0. We have two special cases:





                  • num === 1: this round is singular, next round should be no bottles of juice


                  • num === 0: this round is no bottles of juice, next round should finish the song with Go to the store and buy some more, 99 bottles of juice on the wall. like the original song.





                  singBottles(15, 'juice');

                  function singBottles(givenNum, drinkType) {
                  let num = givenNum;

                  while (num >= 0) {
                  const bottlesFirstLine = num === 0 ?
                  `no more bottles` :
                  `${ num } ${ getPluralState(num) }`;

                  const bottlesSecondLine = num - 1 > 0 ?
                  `${ num - 1 } ${ getPluralState(num - 1) }` :
                  `no more bottles`;

                  const firstLine = `${ bottlesFirstLine } of ${ drinkType } on the wall! ${ bottlesFirstLine } of ${ drinkType }!`;

                  const secondLine = num - 1 >= 0 ? `Take one down, pass it around... ${ bottlesSecondLine } of ${ drinkType } on the wall!` :
                  `Go to the store and buy some more, ${ givenNum } ${ getPluralState(givenNum) } of ${ drinkType } on the wall.`;

                  console.log(`${ firstLine }n${ secondLine }n`);

                  num--;
                  }
                  }

                  function getPluralState(num) {
                  return num === 1 ? 'bottle' : 'bottles';
                  }








                  share|improve this answer




























                    1












                    1








                    1







                    Starting Condition




                    • We start with 99 bottles

                    • Since both 99 and 98 are plural, both this round and next round are set to plural


                    Iteration



                    Everytime we start a new round, we need to check if the current (and next) num is plural or singular



                    Stop Cases



                    The while loop will run until num = 0. We have two special cases:





                    • num === 1: this round is singular, next round should be no bottles of juice


                    • num === 0: this round is no bottles of juice, next round should finish the song with Go to the store and buy some more, 99 bottles of juice on the wall. like the original song.





                    singBottles(15, 'juice');

                    function singBottles(givenNum, drinkType) {
                    let num = givenNum;

                    while (num >= 0) {
                    const bottlesFirstLine = num === 0 ?
                    `no more bottles` :
                    `${ num } ${ getPluralState(num) }`;

                    const bottlesSecondLine = num - 1 > 0 ?
                    `${ num - 1 } ${ getPluralState(num - 1) }` :
                    `no more bottles`;

                    const firstLine = `${ bottlesFirstLine } of ${ drinkType } on the wall! ${ bottlesFirstLine } of ${ drinkType }!`;

                    const secondLine = num - 1 >= 0 ? `Take one down, pass it around... ${ bottlesSecondLine } of ${ drinkType } on the wall!` :
                    `Go to the store and buy some more, ${ givenNum } ${ getPluralState(givenNum) } of ${ drinkType } on the wall.`;

                    console.log(`${ firstLine }n${ secondLine }n`);

                    num--;
                    }
                    }

                    function getPluralState(num) {
                    return num === 1 ? 'bottle' : 'bottles';
                    }








                    share|improve this answer















                    Starting Condition




                    • We start with 99 bottles

                    • Since both 99 and 98 are plural, both this round and next round are set to plural


                    Iteration



                    Everytime we start a new round, we need to check if the current (and next) num is plural or singular



                    Stop Cases



                    The while loop will run until num = 0. We have two special cases:





                    • num === 1: this round is singular, next round should be no bottles of juice


                    • num === 0: this round is no bottles of juice, next round should finish the song with Go to the store and buy some more, 99 bottles of juice on the wall. like the original song.





                    singBottles(15, 'juice');

                    function singBottles(givenNum, drinkType) {
                    let num = givenNum;

                    while (num >= 0) {
                    const bottlesFirstLine = num === 0 ?
                    `no more bottles` :
                    `${ num } ${ getPluralState(num) }`;

                    const bottlesSecondLine = num - 1 > 0 ?
                    `${ num - 1 } ${ getPluralState(num - 1) }` :
                    `no more bottles`;

                    const firstLine = `${ bottlesFirstLine } of ${ drinkType } on the wall! ${ bottlesFirstLine } of ${ drinkType }!`;

                    const secondLine = num - 1 >= 0 ? `Take one down, pass it around... ${ bottlesSecondLine } of ${ drinkType } on the wall!` :
                    `Go to the store and buy some more, ${ givenNum } ${ getPluralState(givenNum) } of ${ drinkType } on the wall.`;

                    console.log(`${ firstLine }n${ secondLine }n`);

                    num--;
                    }
                    }

                    function getPluralState(num) {
                    return num === 1 ? 'bottle' : 'bottles';
                    }








                    singBottles(15, 'juice');

                    function singBottles(givenNum, drinkType) {
                    let num = givenNum;

                    while (num >= 0) {
                    const bottlesFirstLine = num === 0 ?
                    `no more bottles` :
                    `${ num } ${ getPluralState(num) }`;

                    const bottlesSecondLine = num - 1 > 0 ?
                    `${ num - 1 } ${ getPluralState(num - 1) }` :
                    `no more bottles`;

                    const firstLine = `${ bottlesFirstLine } of ${ drinkType } on the wall! ${ bottlesFirstLine } of ${ drinkType }!`;

                    const secondLine = num - 1 >= 0 ? `Take one down, pass it around... ${ bottlesSecondLine } of ${ drinkType } on the wall!` :
                    `Go to the store and buy some more, ${ givenNum } ${ getPluralState(givenNum) } of ${ drinkType } on the wall.`;

                    console.log(`${ firstLine }n${ secondLine }n`);

                    num--;
                    }
                    }

                    function getPluralState(num) {
                    return num === 1 ? 'bottle' : 'bottles';
                    }





                    singBottles(15, 'juice');

                    function singBottles(givenNum, drinkType) {
                    let num = givenNum;

                    while (num >= 0) {
                    const bottlesFirstLine = num === 0 ?
                    `no more bottles` :
                    `${ num } ${ getPluralState(num) }`;

                    const bottlesSecondLine = num - 1 > 0 ?
                    `${ num - 1 } ${ getPluralState(num - 1) }` :
                    `no more bottles`;

                    const firstLine = `${ bottlesFirstLine } of ${ drinkType } on the wall! ${ bottlesFirstLine } of ${ drinkType }!`;

                    const secondLine = num - 1 >= 0 ? `Take one down, pass it around... ${ bottlesSecondLine } of ${ drinkType } on the wall!` :
                    `Go to the store and buy some more, ${ givenNum } ${ getPluralState(givenNum) } of ${ drinkType } on the wall.`;

                    console.log(`${ firstLine }n${ secondLine }n`);

                    num--;
                    }
                    }

                    function getPluralState(num) {
                    return num === 1 ? 'bottle' : 'bottles';
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 27 '18 at 11:29

























                    answered Nov 26 '18 at 17:20









                    ThatkookooguyThatkookooguy

                    2,0551732




                    2,0551732























                        0














                        You missed some parenthesis



                        var bottles = 99; //You need to declare each var like this var x = "x"; var y = "y"
                        var bottle = "bottles"; // or you can declare it like this var x = "x", y ="y"
                        var text = "";

                        var output = document.getElementById('output');
                        while (bottles > 0) {

                        if (bottles == 1) {
                        bottle = "bottle";
                        }//Missed Parenthesis
                        text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>";




                        if (bottles == 0) {
                        bottles = "no more";
                        text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>"
                        }
                        bottles--;//The decreasment is always at the end
                        output.innerHTML += text;
                        }//Missed parenthesis

                        output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


                        http://jsfiddle.net/1wg16qr5/49/






                        share|improve this answer




























                          0














                          You missed some parenthesis



                          var bottles = 99; //You need to declare each var like this var x = "x"; var y = "y"
                          var bottle = "bottles"; // or you can declare it like this var x = "x", y ="y"
                          var text = "";

                          var output = document.getElementById('output');
                          while (bottles > 0) {

                          if (bottles == 1) {
                          bottle = "bottle";
                          }//Missed Parenthesis
                          text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>";




                          if (bottles == 0) {
                          bottles = "no more";
                          text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>"
                          }
                          bottles--;//The decreasment is always at the end
                          output.innerHTML += text;
                          }//Missed parenthesis

                          output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


                          http://jsfiddle.net/1wg16qr5/49/






                          share|improve this answer


























                            0












                            0








                            0







                            You missed some parenthesis



                            var bottles = 99; //You need to declare each var like this var x = "x"; var y = "y"
                            var bottle = "bottles"; // or you can declare it like this var x = "x", y ="y"
                            var text = "";

                            var output = document.getElementById('output');
                            while (bottles > 0) {

                            if (bottles == 1) {
                            bottle = "bottle";
                            }//Missed Parenthesis
                            text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>";




                            if (bottles == 0) {
                            bottles = "no more";
                            text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>"
                            }
                            bottles--;//The decreasment is always at the end
                            output.innerHTML += text;
                            }//Missed parenthesis

                            output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


                            http://jsfiddle.net/1wg16qr5/49/






                            share|improve this answer













                            You missed some parenthesis



                            var bottles = 99; //You need to declare each var like this var x = "x"; var y = "y"
                            var bottle = "bottles"; // or you can declare it like this var x = "x", y ="y"
                            var text = "";

                            var output = document.getElementById('output');
                            while (bottles > 0) {

                            if (bottles == 1) {
                            bottle = "bottle";
                            }//Missed Parenthesis
                            text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>";




                            if (bottles == 0) {
                            bottles = "no more";
                            text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>"
                            }
                            bottles--;//The decreasment is always at the end
                            output.innerHTML += text;
                            }//Missed parenthesis

                            output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>"


                            http://jsfiddle.net/1wg16qr5/49/







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 6 '14 at 10:05









                            Matteo CardelliniMatteo Cardellini

                            20621237




                            20621237























                                0














                                var num = 99;
                                var bottles = " bottles";
                                var bottlesWillChange = " bottles"

                                while (num > 0) {

                                if (num === 2) { bottlesWillChange = " bottle"; }
                                if (num === 1) { bottles = " bottle"; bottlesWillChange = " bottles"; }

                                console.log(num + bottles + " of juice on the wall! " + num + bottles + " of juice! Take one down, pass it around... " + (num - 1) + bottlesWillChange +" of juice on the wall!")

                                num--
                                }


                                Explanation: First we create 3 variables: num, bottles, bottlesWillChange. In the second step we use a while loop with condition num > 0. Main part: if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWillChange variable value to "bottles".






                                share|improve this answer


























                                • Please explain the code(how it works).

                                  – Xcoder
                                  Nov 24 '18 at 23:58











                                • @Xcoder, look, first we create 3 variables:num, bottles, bottlesWillChange. In the second step we declarate while loop with condition num > 0. Main part, if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWiillchange variable value to "bottles". I hope it's clear. when whe need change var

                                  – Andrey Tsapko
                                  Nov 26 '18 at 14:23
















                                0














                                var num = 99;
                                var bottles = " bottles";
                                var bottlesWillChange = " bottles"

                                while (num > 0) {

                                if (num === 2) { bottlesWillChange = " bottle"; }
                                if (num === 1) { bottles = " bottle"; bottlesWillChange = " bottles"; }

                                console.log(num + bottles + " of juice on the wall! " + num + bottles + " of juice! Take one down, pass it around... " + (num - 1) + bottlesWillChange +" of juice on the wall!")

                                num--
                                }


                                Explanation: First we create 3 variables: num, bottles, bottlesWillChange. In the second step we use a while loop with condition num > 0. Main part: if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWillChange variable value to "bottles".






                                share|improve this answer


























                                • Please explain the code(how it works).

                                  – Xcoder
                                  Nov 24 '18 at 23:58











                                • @Xcoder, look, first we create 3 variables:num, bottles, bottlesWillChange. In the second step we declarate while loop with condition num > 0. Main part, if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWiillchange variable value to "bottles". I hope it's clear. when whe need change var

                                  – Andrey Tsapko
                                  Nov 26 '18 at 14:23














                                0












                                0








                                0







                                var num = 99;
                                var bottles = " bottles";
                                var bottlesWillChange = " bottles"

                                while (num > 0) {

                                if (num === 2) { bottlesWillChange = " bottle"; }
                                if (num === 1) { bottles = " bottle"; bottlesWillChange = " bottles"; }

                                console.log(num + bottles + " of juice on the wall! " + num + bottles + " of juice! Take one down, pass it around... " + (num - 1) + bottlesWillChange +" of juice on the wall!")

                                num--
                                }


                                Explanation: First we create 3 variables: num, bottles, bottlesWillChange. In the second step we use a while loop with condition num > 0. Main part: if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWillChange variable value to "bottles".






                                share|improve this answer















                                var num = 99;
                                var bottles = " bottles";
                                var bottlesWillChange = " bottles"

                                while (num > 0) {

                                if (num === 2) { bottlesWillChange = " bottle"; }
                                if (num === 1) { bottles = " bottle"; bottlesWillChange = " bottles"; }

                                console.log(num + bottles + " of juice on the wall! " + num + bottles + " of juice! Take one down, pass it around... " + (num - 1) + bottlesWillChange +" of juice on the wall!")

                                num--
                                }


                                Explanation: First we create 3 variables: num, bottles, bottlesWillChange. In the second step we use a while loop with condition num > 0. Main part: if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWillChange variable value to "bottles".







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 26 '18 at 17:46









                                Xcoder

                                1,01621124




                                1,01621124










                                answered Nov 24 '18 at 23:37









                                Andrey TsapkoAndrey Tsapko

                                114




                                114













                                • Please explain the code(how it works).

                                  – Xcoder
                                  Nov 24 '18 at 23:58











                                • @Xcoder, look, first we create 3 variables:num, bottles, bottlesWillChange. In the second step we declarate while loop with condition num > 0. Main part, if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWiillchange variable value to "bottles". I hope it's clear. when whe need change var

                                  – Andrey Tsapko
                                  Nov 26 '18 at 14:23



















                                • Please explain the code(how it works).

                                  – Xcoder
                                  Nov 24 '18 at 23:58











                                • @Xcoder, look, first we create 3 variables:num, bottles, bottlesWillChange. In the second step we declarate while loop with condition num > 0. Main part, if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWiillchange variable value to "bottles". I hope it's clear. when whe need change var

                                  – Andrey Tsapko
                                  Nov 26 '18 at 14:23

















                                Please explain the code(how it works).

                                – Xcoder
                                Nov 24 '18 at 23:58





                                Please explain the code(how it works).

                                – Xcoder
                                Nov 24 '18 at 23:58













                                @Xcoder, look, first we create 3 variables:num, bottles, bottlesWillChange. In the second step we declarate while loop with condition num > 0. Main part, if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWiillchange variable value to "bottles". I hope it's clear. when whe need change var

                                – Andrey Tsapko
                                Nov 26 '18 at 14:23





                                @Xcoder, look, first we create 3 variables:num, bottles, bottlesWillChange. In the second step we declarate while loop with condition num > 0. Main part, if num === 2, we change value bottlesWillChange to "bottle", because in a part of our song when bottles equal 2, in last part of this string we should use "bottle". Next if statement work only if, our num === 1, we change first and second values of bottles variable to "bottle", because it's single and bottlesWiillchange variable value to "bottles". I hope it's clear. when whe need change var

                                – Andrey Tsapko
                                Nov 26 '18 at 14:23


















                                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%2f25698940%2f99-bottle-of-beer-on-the-wall-code-not-working-pure-js-if-statement-and-while%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







                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()