MusclesGlassez
MusclesGlassez

Reputation: 319

Trouble with a simple while loop

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

Upvotes: 0

Views: 95

Answers (2)

John Ruddell
John Ruddell

Reputation: 25842

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++;
}

Upvotes: 2

Lasitha Benaragama
Lasitha Benaragama

Reputation: 2209

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

Upvotes: 1

Related Questions