【问题标题】:I'm learning JavaScript. What is wrong with my code for a quiz?我正在学习 JavaScript。我的测验代码有什么问题?
【发布时间】:2015-07-15 13:51:24
【问题描述】:
var numofq = 0;
var score = 0;
var questions = [
    "Which has the highest mountain: Earth or Mars?",
    "What are the small indentations on a golf ball called?",
    "Which city has the largest population?",
    "Which country claims the world's tallest building?",
    "With which device are earthquakes recorded?",
    "Name the Yellow Telly Tubby.",
    "What is 'Tiger' Woods's first name?",
    "What are Alpha Centauri and Sirius?",
    "How many men have walked on the moon: 4, 8, or 12?",
    "What is the only word in English ending in the letters 'mt'?",
    "What is the currency of Switzerland?",
    "Who wrote 'Waiting for Godot?'",
    "What is the business term for assets which can be immediately turned into cash?",
    "Which is the largest planet in the solar system?",
    "True or False? Only one word in English rhymes with 'silver'.",
    "Which instrument did Louis Armstrong play?",
    "What is Triskadekaphobia?",
    "On which street do Bert and Ernie live?",
    "Which mythological figure flew so close to the sun that the wax on his wings began to melt?",
    "What is the green pigment in plants called?"
];


var answers = ["Mars", "dimples", "Tokyo", "Malaysia", "seismograph", "La La", "Eldrick", "stars", "12", "dreamt", "Swiss Franc", "Samuel Beckett", "liquid assets", "Jupiter", "False, none do", "trumpet", "the fear of the number 13", "Sesame Street", "Icarus", "chloryphyll"];

function askQuestion() {
    for (var i = 0; i < 10; i++) {
        var number = Math.floor((Math.random() * 10) + 1);
        var ask = prompt(questions[number]);

        if (ask === answers[number]) {

            score++;

            numofq++;

            alert("CORRECT! You have got " + score + " out of " + numofq + " questions correct so far.");

        } else {

            numofq++;

            alert("INCORRECT!You have got " + score + " out of " + numofq + " questions correct so far. The correct answer is " + answer[number]);

        } //End of for

        var percent = (score / numofq) * 100;

        if (numofq === 10) {

            alert("You got " + percent + "%");

        }

    } // End of function

}


askQuestion();

【问题讨论】:

  • @BrantOlsen:如果您要修复帖子,请修复所有其中的问题。
  • 不要在代码块内使用粗体标签
  • 什么不起作用?请提供详细信息?控制台有错误吗?
  • 您在控制台Uncaught ReferenceError: answer is not defined 中收到此错误。尝试将这一行 alert("INCORRECT!You have got " + score + " out of " + numofq + " questions correct so far. The correct answer is " + answer[number]); 更改为 answers[number]
  • @Joel Almeida 非常感谢。它现在正在工作

标签: javascript


【解决方案1】:

您在控制台中遇到错误:

Uncaught ReferenceError: answer is not defined

改变这一行:

alert("INCORRECT!You have got " + score + " out of " + numofq + 
" questions correct so far. The correct answer is " + answer[number]);

拥有answers。像这样:

alert("INCORRECT!You have got " + score + " out of " + numofq + 
" questions correct so far. The correct answer is " + answers[number]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2017-01-21
    • 2022-11-17
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多