[英]Javascript For Loop and Function won't return anything
當我執行此頁面時,沒有提示出現。 我想讓它起作用嗎? 我對JavaScript相對較新,因此不勝感激!
function calc(correct, answer) {
if (correct == answer) {
alert("You hit the jackpot!");
} else {
alert("WRONG! the correct answer is: " + answer);
}
}
var questions = ["2 x 10", "5 + 15", "25 - 20", "100 / 10", "9 x 2", "20 + 3", "5 - 5", "5 / 10", "1 + 11111", "0 + 14"];
var answers = [ 2 * 10, 5 + 15, 25 - 20, 100 / 10, 9 * 2, 20 + 3, 5 - 5, 5 / 10, 1 + 11111, 0 + 14 ];
for (var i=0; i<10; i++) {
var answer = promt(questions[i]);
calc(answer, answers[i]);
}
這是您的錯字:提示而不是提示
您應該使用“提示”。 並且在調用Calc方法之前,您還應該檢查null。
嘗試這個:
var questions = ["2 x 10", "5 + 15", "25 - 20", "100 / 10", "9 x 2", "20 + 3", "5 - 5", "5 / 10", "1 + 11111", "0 + 14"];
var answers = [ 2 * 10, 5 + 15, 25 - 20, 100 / 10, 9 * 2, 20 + 3, 5 - 5, 5 / 10, 1 + 11111, 0 + 14 ];
for (var i=0; i<10; i++) {
(answers[i] == prompt(questions[i])) ?
alert("You hit the jackpot!") :
alert("WRONG! the correct answer is: " + answers[i])
}
除了您注意到其他打字錯誤prompt
之外,您的游戲流程中可能還存在問題,因為您的for loop
可能僅提示您一次並退出。
這里有一些更有趣的事情: 演示
var i = 0,
playerScore = 0,
QA = [
["2 x 10" , 2*10, 2], //["Q", A, score-points-value]
["5 + 15" , 5+15, 1],
["25 - 20", 25-20, 1]
];
function calc() {
var question = QA[i][0];
var answer = QA[i][1];
var points = QA[i][2];
var correct = answer == prompt("Question for "+ points +" points:\n"+ question );
if(correct){
playerScore += points;
alert("Great! Your score is: "+ playerScore);
}else{
alert("Wrong, the correct answer is "+ answer );
}
calc(i++); // Move on with the game
}
calc(); // Start
您輸入了“提示”功能的名稱錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.