Javascript For循环和函数不会返回任何内容

时间:2014-03-05 10:54:06

标签: javascript html function for-loop

当我执行此页面时,没有显示任何提示。为了让这个工作,我错过了什么? 我对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]);
}

5 个答案:

答案 0 :(得分:0)

这是你的错字:提示,而不是承诺

答案 1 :(得分:0)

你应该使用'提示'。在调用Calc方法之前,您还应该检查null。

答案 2 :(得分:0)

试试这个:

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])
}

答案 3 :(得分:0)

除了你注意到其他错误prompt之外,你的游戏流程可能会出现问题,因为for loop可能只会提示你一次并退出。

以下是更有趣和有趣的内容: 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 

答案 4 :(得分:-1)

您错误输入了“提示”功能的名称。