2

I need to execute a if/else conditional inside a JS 'for' loop. Specifically, this is what I'm told to do: http://i.imgur.com/lA1mRGp.jpg

Here is my code

var numSheep = 4;
var monthsToPrint = 12;
for (var monthNumber = 1; monthNumber <= monthsToPrint; monthNumber++) {
  if (numSheep < 10000) {
    numSheep *= 4;
    console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
  } else {
    numSheep *= 4;
    var number = numSheep / 2;
    var newNum = numSheep - number;
    console.log("Removing " + number + " sheep from the population. Phew!");
    console.log("There will be " + newNum + " sheep after " + monthNumber + " month(s)!");
  }
}

Everytime I submit this code I get this error from codeschool, "You're calling console.log the correct number of times, but not logging the correct messages. Are you diving the numSheep by 2 whenever there are more than 10000."

P.S: It's challenge no. 5 of JavaScript Road Trip Part 2 on codeschool. If you need to log into codeschool and see for yourself, this link should give a 48 hour access: http://go.codeschool.com/eO1V6A

1
  • @eosterberg Hi, that doesn't change anything unfortunately. It prints out the same things as before. Commented Dec 7, 2013 at 13:47

1 Answer 1

2

If I got everything correct

  • First of all as @eosterberg mentioned in a comment the text says "for any month the population is above 10,000"
  • Secondly you need to save the number of sheep you have left; numSheep = numSheep/2, the sheep you sent away is right now the same but store that in a variable, sentSheep.
  • Thirdly the text says; "The rate at which the staying population is grows, however, will stay the same(x4)" this means that the population that stays only grows(at least what we know) => you should put numSheep *= 4; after the calculations and before the console.log's

And the actual code would look like;

var numSheep = 4;
var monthsToPrint = 12;
for (var monthNumber = 1; monthNumber <= monthsToPrint; monthNumber++) {
  if (numSheep <= 10000) {
    numSheep *= 4;
    console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
  } else {
    numSheep = numSheep / 2;
    var sentSheep = numSheep;
    numSheep *= 4;
    console.log("Removing " + sentSheep + " sheep from the population. Phew!");
    console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Happy to help ;) Do you understand everything @user973282, so you learned something from it?
I hope I understand. The first mistake I made was, I didn't store the new value of sheeps after removing half off it. I was just calculating it and as, I didn't save any new data the calculations were wrong when we started to remove sheeps!! Also, as the growth should be calculated after we take away sheeps, the numSheep *= 4; should be after we remove the required no. of sheeps. Did I get it right? :)
Yeah pretty much, you missed the <=10000 thing otherwise it looks good. Good luck with learning JS ;)

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.