0

I'm sorry to be the one to ask a trivial question, I don't like wasting anyone's time. I'm new in the Javascript game and I'm trying to crash course myself in before I start up in school again.

I have a simple while loop which is as follows:

var numSheep = 4;
var monthNumber = 1;
var monthsToPrint = 12;

while (monthNumber <= 12){
    console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
    monthNumber++;
    numSheep = numSheep * 4;
}

The first line prints There will be 4 sheep after 1 month(s)!
But I'd like it to multiply numsSheep by 4, before printing it for the first time (so it'll be 16 when console.log() is called for the first time).

I know this is probably a stupid question and I'm overlooking something simple, but I'm stuck here

6
  • 2
    Are you saying that you want to print "numsSheep is a multiple of 4" before going into the while loop? Commented Apr 2, 2014 at 3:01
  • 4 is a multiple of 4, I don't see anything wrong. Commented Apr 2, 2014 at 3:02
  • Or are you saying that you want to increase the number of sheep by 4 every month, instead of multiplying by 4? (both methods only yield multiples of 4!) Commented Apr 2, 2014 at 3:02
  • I want it to multiply by 4 every month, and I want it to start the multiple on the first console print out, not the second. Commented Apr 2, 2014 at 3:03
  • 2
    then do the calculation before you do your console.log Commented Apr 2, 2014 at 3:04

2 Answers 2

2

if i understand your question correctly you just need to do the multiplication before you do your console.log

var numSheep = 4,
    monthNumber = 1,
    monthsToPrint = 12;

while (monthNumber <= 12){
    numSheep = numSheep * 4;    
    console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
    monthNumber++;
}
Sign up to request clarification or add additional context in comments.

Comments

1
while (monthNumber <= 12){
    numSheep = numSheep * 4;    
    console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
        monthNumber++;
    }

Comments

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.