var result = 1;
var counter = 0;
while (counter < 10) {
result = result * 2;
counter = counter + 1;
}
alert(result);
The counter could also start at 1 and check for <= 10, but, for reasons that will become apparent later on, it is a good idea to get used to counting from 0.
Obviously, your own solutions aren’t required to be precisely the same as mine. They should work. And if they are very different, make sure you also understand my solution.
var result = 1;
for (var counter = 0; counter < 10; counter = counter + 1)
result = result * 2;
alert(result);
Note that even if no block is opened with a ‘{‘, the statement in the loop is still indented four spaces to make it clear that it “belongs” to the line above it.
Math.floor(n.d) returns the integer part (n) of a number with integer part n and decimal part .d.
Math.random() returns a random number in the interval [0, 1).
var low = 1;
var high = 10;
alert(Math.floor((high - low + 1) * Math.random() + low));
var low = 1;
var high = 10;
var opr1 = Math.floor((high - low + 1) * Math.random() + low);
var opr2 = Math.floor((high - low + 1) * Math.random() + low);
var question = "What is the value of " + opr1 + " + " + opr2 + "?";
var answer;
while (true) {
answer = prompt(question);
if (answer == opr1 + opr2) {
alert("You must be a genius or something.");
break;
}
else if (answer == opr1 + opr2 + 1 || answer == opr1 + opr2 -1)
alert("Almost!");
else
alert("You're an embarrassment.");
}