Skip to content

Instantly share code, notes, and snippets.

@duggiemitchell
Created January 12, 2016 08:24
Show Gist options
  • Select an option

  • Save duggiemitchell/310fd2a949f5e821859f to your computer and use it in GitHub Desktop.

Select an option

Save duggiemitchell/310fd2a949f5e821859f to your computer and use it in GitHub Desktop.
Back at Death Valley, scientists could see that the Sheep Situation would quickly get out of control. They have decided that, for any month the population climbs above 10000, half of the sheep will be sent away to other regions.
Inside our for loop, insert an if statement that:
Removes half of the sheep population if the number of sheep rises above 10000.
Prints the number of sheep being removed to the console in the following format:
Removing <number> sheep from the population.
Note: To complete the challenge, you only need to insert the if statement. You do not need to create an else statement or change any of the provided code.
Code School
var numSheep = 4;
var monthsToPrint = 12;
for (var monthNumber = 1; monthNumber <= monthsToPrint; monthNumber++) {
if (numSheep > 10000) {
numSheep = numSheep / 2;
console.log ("Removing " + numSheep + " sheep from the population.");
}
numSheep *= 4;
console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
}
// Ouput:
/*
There will be 16 sheep after 1 month(s)!
There will be 64 sheep after 2 month(s)!
There will be 256 sheep after 3 month(s)!
There will be 1024 sheep after 4 month(s)!
There will be 4096 sheep after 5 month(s)!
There will be 16384 sheep after 6 month(s)!
Removing 8192 sheep from the population.
There will be 32768 sheep after 7 month(s)!
Removing 16384 sheep from the population.
There will be 65536 sheep after 8 month(s)!
Removing 32768 sheep from the population.
There will be 131072 sheep after 9 month(s)!
Removing 65536 sheep from the population.
There will be 262144 sheep after 10 month(s)!
Removing 131072 sheep from the population.
There will be 524288 sheep after 11 month(s)!
Removing 262144 sheep from the population.
There will be 1048576 sheep after 12 month(s)!*/

Back at Death Valley, scientists could see that the Sheep Situation would quickly get out of control. They have decided that, for any month the population climbs above 10000, half of the sheep will be sent away to other regions.

Inside our for loop, insert an if statement that:

Removes half of the sheep population if the number of sheep rises above 10000. Prints the number of sheep being removed to the console in the following format: Removing <number> sheep from the population.

var numSheep = 4;
var monthsToPrint = 12;

for (var monthNumber = 1; monthNumber <= monthsToPrint; monthNumber++) {

if (numSheep > 10000) {
    numSheep = numSheep / 2;
    console.log ("Removing " + numSheep +  " sheep from the population.");
  } 
  numSheep *= 4;
  console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
}
// Ouput: There will be 16 sheep after 1 month(s)!
There will be 64 sheep after 2 month(s)!
There will be 256 sheep after 3 month(s)!
There will be 1024 sheep after 4 month(s)!
There will be 4096 sheep after 5 month(s)!
There will be 16384 sheep after 6 month(s)!
Removing 8192 sheep from the population.
There will be 32768 sheep after 7 month(s)!
Removing 16384 sheep from the population.
There will be 65536 sheep after 8 month(s)!
Removing 32768 sheep from the population.
There will be 131072 sheep after 9 month(s)!
Removing 65536 sheep from the population.
There will be 262144 sheep after 10 month(s)!
Removing 131072 sheep from the population.
There will be 524288 sheep after 11 month(s)!
Removing 262144 sheep from the population.
There will be 1048576 sheep after 12 month(s)!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment