以下代码来自我的课程,但无法正常工作。
我试图尽我所能想到的一切
var eatSteak = confirm("Do you eat steak?");
var confirmSteak = confirm("You like Steak!");
var confirmNoSteak = confirm("Here's a tofu stir-fry");
var howOld = prompt("How old are you?");
var howNotOld = confirm("No Sake for you!");
var sakeForYou = confirm("Sake for you!");
var tryAgain = confirm("TRY AGAIN!");
if (eatSteak == true) {
alert(confirmSteak);
} else {
alert(confirmNoSteak);
}
if (howOld < 21) {
alert(howNotOld);
} else if (howOld >= 21) {
alert(sakeForYou);
} else {
alert(tryAgain);
}
控制台未显示任何错误消息
答案 0 :(得分:1)
confirm()
仅应用于提问。如果只想为消息设置变量,则不应调用confirm()
。像这样的语句
var confirmSteak = confirm("You like Steak!");
应该是
var confirmSteak = "You like Steak!";
此外,当您测试howOld
时,请检查它们是否小于21,然后检查它们是否至少为21。这是仅有的两种可能性,它永远都不可能到达最后else
块。因此,您可以只使用if
和else
-不论它们是否清酒。
var eatSteak = confirm("Do you eat steak?");
var confirmSteak = "You like Steak!";
var confirmNoSteak = "Here's a tofu stir-fry";
var howOld = prompt("How old are you?");
var howNotOld = "No Sake for you!";
var sakeForYou = "Sake for you!";
if (eatSteak == true) {
alert(confirmSteak);
} else {
alert(confirmNoSteak);
}
if (howOld < 21) {
alert(howNotOld);
} else {
alert(sakeForYou);
}
答案 1 :(得分:0)
一个简单的规则。仅在要获得用户输入时才使用prompt
和confirm
。
if (confirm("Do you eat steak?") == true)
alert('You like Steak!')
else
alert('Heres a tofu stir-fry');
//Here is a variable which stores the user input
var age = prompt("How old are you?");
if (age < 21)
alert('No Sake for you!')
else
if (age > 21)
alert('Sake for you!')
else
alert('TRY AGAIN!');