1

I am very new to coding and it's been fun so far. But I've been trying to work a function and for loop together and have it print out with console.log. Is anything at all correct? Or am I just completely lost?

What I want it to do is have bottlesLeft have a value of 99. Declare a function named 'bottleStockReporter'. Inside the function use console.log() to print out how many bottles of root beer are on the wall. "99 bottles of root beer on the wall. 99 bottles of root beer." Use a while loop to call your new function for the value of 'bottlesLeft' and decrease 'bottlesLeft' by 1 each time.

This is what I have so far:

var bottleStockReporter = function(number)
{
    var bottlesLeft = 99;
    console.log(bottlesLeft) + "bottles of root beer on the wall." + (bottlesLeft) + "bottles of root beer on the wall.";
    for(var bottlesLeft = 99; bottleLeft>0; bottlesLeft = bottlesLeft --);
};
bottleStockReporter(99);

my output prints out the number 99

4
  • 2
    thats not Java code, it looks like Javascript. I've edited your tags. Commented Aug 20, 2015 at 16:51
  • No, I'm not in school anymore. But I am trying to answer study questions to learn to code. I have been using Code school/code academy/ looking at online resources/etc Commented Aug 20, 2015 at 16:59
  • Well if the solution requires you to use a while loop then you shouldn't be using a for loop. Commented Aug 20, 2015 at 17:02
  • If you're learning, you might appreciate a good documentation. Commented Aug 20, 2015 at 17:04

4 Answers 4

1

Easy solution. Basically, you pass a number, bottlesLeft is set to that number, and we loop downwards and output to the console.

Here's your challenge: Test this function. What if you pass a non-number? What if a string is passed? What if a negative number is passed? How do you account for these cases?

var bottleStockReporter = function(number) {
  for(var bottlesLeft = number; bottlesLeft>0; bottlesLeft--) {
    console.log(bottlesLeft + " bottles of root beer on the wall. " + (bottlesLeft) + " bottles of root beer on the wall.");
  }
};

bottleStockReporter(99); //Loops 99 times
Sign up to request clarification or add additional context in comments.

Comments

1

You should probably change var bottlesLeft = 99; to var bottlesLeft = number;. This way, when you call bottleStockReporter(some number);, bottlesLeft will not be set to 99 every time. In addition, your for loop will set bottlesLeft to 99 again, so you should change it to look like:

for (var num = bottlesLeft; num >= 0; num--) {
    console.log(num + " bottles of beer on the wall. " + num + " bottles of beer.");
}

In the end, something like this:

function bottleStockReporter(number) {
    for (var num = number; num >= 0; num--) {
        console.log(num + " bottles of root beer on the wall.");
    }
}
bottleStockReporter(99);

If you want to be extra fancy and not output 1 bottles of beer you could do:

function bottleStockReporter(number) {
    for (var num = number; num >= 0; num--) {
        var str = " bottles ";
        if (num == 1) {
            str = " bottle ";
        }
        console.log(num + str + "of root beer on the wall.");
    }
}
bottleStockReporter(99);

Comments

1

You should first start by following the problem statement.

Declare a function named bottleStockReporter

function bottleStockReporter() {

}

Use console.log to print out how many bottles of root on the wall.

Kind of ambigous, but lets assume that the function bottleStockReporter takes as a parameter the number of bottles left:

function bottleStockReporter(bottlesLeft) {
    console.log(bottlesLeft + " bottles of beer on the wall. " + bottlesLeft + " bottles of beer.");  
}

Use a while loop to call your new function for the value of 'bottlesLeft' and decrease 'bottlesLeft' by 1 each time.

Assuming there are 99 bottles:

var bottles = 99;
while(bottles > 0) { //While loop. Call the function while bottles is positive.
   bottleStockReporter(bottles); //Call function 'bottles' times
   bottles--; //Decrease bottles
}

And there you go.

Comments

0

But what about the grammatical concordance?

function bottleStockReporter(i) {
    for(var i = 99; i>0; i--) {
        var bottleOrBottles = (i > 1) ? " bottles" : " bottle";         
        console.log(i + bottleOrBottles + " of root beer on the wall.");            
    }
}

bottleStockReporter(99);

2 Comments

Thank you everyone for all the great advice and links. I still have a loooong way to go but it's exciting! thanks!
No problem: you are beginning in the right way. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.