简单的 JavaScript 函数只要求用户正确回答 5 次?
Simple JavaScript function that asks the user for correct answer only 5 times?
我正在尝试使用 JavaScript 开发一个非常简单的函数,提示用户输入一个数字,然后给他们 5 次机会来猜测给定数字的平方。
因此,例如,如果用户在第一个提示中输入 6,他们应该在第二个提示中输入 36,但如果他们没有正确输入,他们会收到一个错误消息,提示猜测的数字是错误的。而且它们仅限于 5 次机会,因此之后,程序不会再次提示用户。
为了简单起见,我尝试做这样的事情:
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries");
if (input2 == input*input) {
alert("Good!");
} else {
prompt("Wrong, enter again!");
}
我走的路对吗?我的意思是它没有做我想让它做的事,但我真的被困在这一点上。不知道如何将其循环 5 次,或下一步该做什么。
您缺少右括号:
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries")); //<--- here
if (input2 == input*input) {
alert("Good!");
} else {
prompt("Wrong, enter again!");
}
...你需要一个循环。最简单的理解就是for
:
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries"));
for (var i = 0; i < 5; i++) {
if (input2 == input*input) {
alert("Good!");
i = 5;
} else {
input2 = prompt("Wrong, enter again!")
}
}
使用do-while
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries"));
var tries = 1;
do {
if (input2 == input * input) {
alert("Good!");
break;
} else {
prompt("Wrong, enter again!");
}
} while (++tries < 5);
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries"));
var tries = 0;
do {
if (input2 == input * input) {
alert("Good!");
break;
} else {
input2 = parseInt(window.prompt("Wrong, enter again!"));
}
} while (++tries < 5);
试试这个
function guessSquare() {
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var c = 5;
var message = "Guess its square now in 5 tries";
(function receiveAnswer() {
var input2 = parseInt(window.prompt(message));
if (input2 == input * input) {
alert("Good!");
} else {
c--;
if (c === 0) {
alert("Ran out of attempts!");
} else {
message = "Wrong, enter again! " + c + " attempts left!";
receiveAnswer();
}
}
})();
}
我正在尝试使用 JavaScript 开发一个非常简单的函数,提示用户输入一个数字,然后给他们 5 次机会来猜测给定数字的平方。
因此,例如,如果用户在第一个提示中输入 6,他们应该在第二个提示中输入 36,但如果他们没有正确输入,他们会收到一个错误消息,提示猜测的数字是错误的。而且它们仅限于 5 次机会,因此之后,程序不会再次提示用户。
为了简单起见,我尝试做这样的事情:
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries");
if (input2 == input*input) {
alert("Good!");
} else {
prompt("Wrong, enter again!");
}
我走的路对吗?我的意思是它没有做我想让它做的事,但我真的被困在这一点上。不知道如何将其循环 5 次,或下一步该做什么。
您缺少右括号:
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries")); //<--- here
if (input2 == input*input) {
alert("Good!");
} else {
prompt("Wrong, enter again!");
}
...你需要一个循环。最简单的理解就是for
:
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries"));
for (var i = 0; i < 5; i++) {
if (input2 == input*input) {
alert("Good!");
i = 5;
} else {
input2 = prompt("Wrong, enter again!")
}
}
使用do-while
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries"));
var tries = 1;
do {
if (input2 == input * input) {
alert("Good!");
break;
} else {
prompt("Wrong, enter again!");
}
} while (++tries < 5);
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var input2 = parseInt(window.prompt("Guess its square now in 5 tries"));
var tries = 0;
do {
if (input2 == input * input) {
alert("Good!");
break;
} else {
input2 = parseInt(window.prompt("Wrong, enter again!"));
}
} while (++tries < 5);
试试这个
function guessSquare() {
var input = parseInt(window.prompt("Enter a number", "Enter here"));
var c = 5;
var message = "Guess its square now in 5 tries";
(function receiveAnswer() {
var input2 = parseInt(window.prompt(message));
if (input2 == input * input) {
alert("Good!");
} else {
c--;
if (c === 0) {
alert("Ran out of attempts!");
} else {
message = "Wrong, enter again! " + c + " attempts left!";
receiveAnswer();
}
}
})();
}