20·6
2016
I created this small "Odds and Evens" game to practice while loops and take a shot at using modulo.
// set the initial value of trickQuestion to 0, so the while loops at least once.
var trickQuestion = 0;
console.log("Welcome to Odds and Evens.");
// set the loop to run as long as the random number is divisible by 2
while (trickQuestion % 2 === 0) {
// on the first round calculate a new value for trickQuestion
if (trickQuestion === 0) {
trickQuestion = Math.ceil(Math.random() * 100);
}
// on each win congratulate the user and calculate a new value for trickQuestion
else {
console.log("You landed on " + trickQuestion + ", you win!");
trickQuestion = Math.ceil(Math.random() * 100);
}
}
// if trickQuestion is not divisible by 2 display the following message
console.log("You landed on " + trickQuestion + ", You lose!");