亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何根據用戶選擇在 JS 中再次重復我的程序?

如何根據用戶選擇在 JS 中再次重復我的程序?

月關寶盒 2022-12-29 16:34:36
好的,我使用 html、css 和 JS 做了一個小測驗。我想做的是在測驗結束時,我想請用戶選擇是否愿意再次進行測驗。我想我必須使用“while”循環,但不太確定。好的,我已經添加了 html 和 JS。我認為這很容易理解。我已經添加了事件監聽器,但它運行不正常,我不知道如何修復它。它給我錯誤消息說“addEventListener 不是函數”。function Quiz(questions) {    this.score = 0;    this.questions = questions;    this.questionIndex = 0;}Quiz.prototype.getQuestionIndex = function() {    return this.questions[this.questionIndex];}Quiz.prototype.guess = function(answer) {    if(this.getQuestionIndex().isCorrectAnswer(answer)) {        this.score++;    }    this.questionIndex++;}Quiz.prototype.isEnded = function() {    return this.questionIndex === this.questions.length;}function Question(text, choices, answer) {    this.text = text;    this.choices = choices;    this.answer = answer;}Question.prototype.isCorrectAnswer = function(choice) {    return this.answer === choice;}function populate() {    if(quiz.isEnded()) {        showScores();    }    else {        // show question        var element = document.getElementById("question");        element.innerHTML = quiz.getQuestionIndex().text;        // show options        var choices = quiz.getQuestionIndex().choices;        for(var i = 0; i < choices.length; i++) {            var element = document.getElementById("choice" + i);            element.innerHTML = choices[i];            guess("btn" + i, choices[i]);        }        showProgress();    }};function guess(id, guess) {    var button = document.getElementById(id);    button.onclick = function() {        quiz.guess(guess);        populate();    }};function showProgress() {    var currentQuestionNumber = quiz.questionIndex + 1;    var element = document.getElementById("progress");    element.innerHTML = "Question " + currentQuestionNumber + " of " + quiz.questions.length;};
查看完整描述

1 回答

?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

我添加了一個<div id="results">(默認情況下隱藏),用于顯示結果。當游戲結束時,showScores()函數會顯示它<div>并隱藏<div id="quiz">.

當用戶單擊Restart按鈕時,處理程序會隱藏<div id="results">并顯示,<div id="quiz">以便可以玩游戲。

這可以通過添加/刪除類而不是直接操作 CSS (style.display屬性) 來改進。但這留給讀者作為練習。

您還可以在此處找到此代碼:https ://jsfiddle.net/kjLbea61/

function Quiz(questions) {

    this.score = 0;

    this.questions = questions;

    this.questionIndex = 0;

}


Quiz.prototype.getQuestionIndex = function() {

    return this.questions[this.questionIndex];

}


Quiz.prototype.guess = function(answer) {

    if(this.getQuestionIndex().isCorrectAnswer(answer)) {

        this.score++;

    }


    this.questionIndex++;

}


Quiz.prototype.isEnded = function() {

    return this.questionIndex === this.questions.length;

}



function Question(text, choices, answer) {

    this.text = text;

    this.choices = choices;

    this.answer = answer;

}


Question.prototype.isCorrectAnswer = function(choice) {

    return this.answer === choice;

}



function populate() {

    if(quiz.isEnded()) {

        showScores();

    }

    else {

        // show question

        var element = document.getElementById("question");

        element.innerHTML = quiz.getQuestionIndex().text;


        // show options

        var choices = quiz.getQuestionIndex().choices;

        for(var i = 0; i < choices.length; i++) {

            var element = document.getElementById("choice" + i);

            element.innerHTML = choices[i];

            guess("btn" + i, choices[i]);

        }


        showProgress();

    }

};


function guess(id, guess) {

    var button = document.getElementById(id);

    button.onclick = function() {

        quiz.guess(guess);

        populate();

    }

};



function showProgress() {

    var currentQuestionNumber = quiz.questionIndex + 1;

    var element = document.getElementById("progress");

    element.innerHTML = "Question " + currentQuestionNumber + " of " + quiz.questions.length;

};


function shuffle_questions(questions) {

    var currentIndex = questions.length, temporaryValue, randomIndex;


    while (0 !== currentIndex) {

        randomIndex = Math.floor(Math.random() * currentIndex);

        currentIndex -= 1;


        temporaryValue = questions[currentIndex];

        questions[currentIndex] = questions[randomIndex];

        questions[randomIndex] = temporaryValue;

    }


    return questions;

}


function restart() {

    questions = shuffle_questions(questions); // Left as an exercise for the reader; see https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array

    quiz = new Quiz(questions); // Rebuild the quiz object

    populate();

    

    document.getElementById('quiz').style.display = 'block'; // show quiz

    document.getElementById('results').style.display = 'none'; // hide results

  }


function showScores() {

    document.getElementById('quiz').style.display = 'none'; // hide quiz

    document.getElementById('results').style.display = 'block'; // show results


    document.getElementById('score').innerHTML = quiz.score; // Put score in results

};


// create questions here

var questions = [

    new Question("Which nation won FIFA 2018 World Cup?", ["Peru", "France","Germany", "USA"], "France"),

    new Question("Which nation hosted FIFA 2018 World Cup?", ["Sweden", "Russia", "Iran", "South Korea"], "Russia"),

    new Question("Which nation has won the most World Cups?", ["Argentina", "Peru","Brazil", "France"], "Brazil"),

    new Question("Where was FIFA 2014 World Cup hosted?", ["Ecuador", "Brazil", "France", "All"], "Brazil"),

    new Question("Which nation won the first FIFA World Cup", ["Brazil", "Uruguay", "Italy", "Australia"], "Uruguay")

];


document.getElementById('restart').addEventListener('click', restart);


// create quiz

var quiz = new Quiz(questions);


// display quiz

populate();

<!DOCTYPE html>


<html>

<head>

    <meta charset="UTF-8">

    <title>Quiz</title>

    <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

    <div class="grid">

        <div id="quiz">

            <h1>FIFA World Cup Quiz</h1>

            <hr style="margin-bottom: 20px">


            <p id="question"></p>


            <div class="buttons">

                <button id="btn0"><span id="choice0"></span></button>

                <button id="btn1"><span id="choice1"></span></button>

                <button id="btn2"><span id="choice2"></span></button>

                <button id="btn3"><span id="choice3"></span></button>

            </div>


            <hr style="margin-top: 50px">

            <footer>

                <p id="progress">Question x of y</p>

            </footer>

        </div>

        <div id="results" style="display: none">

          <h1>

            Results

          </h1>

          <h2>

            Your scores: <span id="score"></span>

          </h2>

          <button id="restart">

            Restart

          </button>

        </div>

        

    </div>



<!--

<script src="question.js"></script>

-->


</body>

</html>


查看完整回答
反對 回復 2022-12-29
  • 1 回答
  • 0 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號