[英]Javascript For Loop and Function won't return anything
When i execute this page no promts are showing up. 当我执行此页面时,没有提示出现。 What am I missing to get this to work? 我想让它起作用吗? I'm relatively new to JavaScript so any kind of help would be appreciated! 我对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]);
}
这是您的错字:提示而不是提示
You should use 'prompt'. 您应该使用“提示”。 And before calling to Calc method you should also check for null. 并且在调用Calc方法之前,您还应该检查null。
Try this: 尝试这个:
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])
}
Beside that you have the typo prompt
other noticed, you might have issues in your game-flow cause your for loop
might prompt you only once and exit. 除了您注意到其他打字错误prompt
之外,您的游戏流程中可能还存在问题,因为您的for loop
可能仅提示您一次并退出。
Here's something more fun and interesting: demo 这里有一些更有趣的事情: 演示
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.