user443946
user443946

Reputation: 845

General Looping question

var answer = " ";
var correct = "4";
var question = "What is 2 * 2?";

for(i = 2; i < 5; i++) {
    answer = prompt(question, "0");
    if (answer == correct) {
        alert("Your answer is correct!");
        break;
    }
}

How many chances would the user have before the break command got executed?

Upvotes: 0

Views: 95

Answers (2)

Brian R. Bondy
Brian R. Bondy

Reputation: 347216

The user would have 3 chances unless they guessed right on the 1st or 2nd chance. Then they would only have 1 or 2 chances respectively.

i = 2 in the first iteration, i = 3 in the next iteration and i = 4 in the last iteration. Before another iteration starts i is set to 5 and the condition fails.

Upvotes: 4

Topera
Topera

Reputation: 12389

The user will have 3 chances (i=2, i=3 and i=4).

Upvotes: 2

Related Questions