1. While we're at it WHy does the Variable line repeat twice?


#1

var coinFace = Math.floor(Math.random() * 2);

while(coinFace === 0){
console.log("Heads! Flipping again...");
var coinFace = Math.floor(Math.random() * 2);
}
console.log("Tails! Done flipping.");

why does the variable line for "coinFace" repeat twice?


#2

coinFace has two possible values: 0 and 1

0 represents the head side of a coin, 1 the tails side.

The code will repeat for as long till we have tails. So the loop could run any number of times, this can be twice, the next time it can be different


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.