Javascript / jQuery琐事游戏 - 不显示下一个?在数组中

时间:2018-06-16 14:08:58

标签: javascript jquery

好的人,这是我遇到的问题 -

我正在使用jQuery创建一个琐事游戏。第一个问题和一组答案是加载,正确/和错误的答案被分配"到正确的按钮。

然而,游戏并没有超越第一个问题。我的想法是我的另外一个问题"接下来的问题"函数在嵌套的for循环之外,这不在正确的范围内,以便知道移动到数组中的下一个问题。我最初有我的代码,所以它是随机化数组中的问题,但后来我重复问题,我对尝试解决问题感到沮丧,所以我尝试了别的东西。

实际上,要想到它 - 显示的第一个问题实际上是问题数组中的最后一个项目。

你可以在这里查看游戏 - https://alyciamriley.github.io/atomic-trivia/ 回购在这里 - https://github.com/AlyciamRiley/atomic-trivia

我也包含了我的代码。



window.onload = function() {
  let intervalId;
  let timer = 10;
  //need set of questions

  let questionArray = [
    {
      question:
        "Who recorded the original version of the song "Hound Dog"?",
      answers: [
        'Willa Mae "Big Mama" Thornton',
        "Elvis Presley",
        "Carl Perkins",
        "Chuck Berry",
        "Miley Cyrus"
      ],
      correctAnswer: 'Willa Mae "Big Mama" Thornton'
    },
    {
      question:
        "Who was marketed by her record company as the "Female Elvis"?",
      answers: [
        "Wanda Jackson",
        "Janis Martin",
        "Patsy Cline",
        "Diana Ross",
        "Miley Cyrus"
      ],
      correctAnswer: "Janis Martin"
    },
    {
      question:
        "Who sang the 1957 song "Whole Lotta Shakin Goin On"?",
      answers: [
        "Elvis Presley",
        "Jerry Lee Lewis",
        "Gene Vincent",
        "Buddy Holly",
        "Miley Cyrus"
      ],
      correctAnswer: "Jerry Lee Lewis"
    },
    {
      question:
        ""Rebel-Rouser", "Cannonball", and "Because They Are Young" were all hits by which artist?",
      answers: [
        "The Big Bopper",
        "Jerry Lee Lewis",
        "Gene Vincent",
        "Duane Eddy",
        "Miley Cyrus"
      ],
      correctAnswer: "Duane Eddy"
    },

    {
      question:
        "Who spent three weeks at No.1 in 1957 with "That’ll be the Day"?",
      answers: [
        "Buddy Holly",
        "June Carter",
        "Little Richard",
        "Fats Domino",
        "Miley Cyrus"
      ],
      correctAnswer: "Buddy Holly"
    }
  ];

  // Needed functions--

  //start game (start the timer, get it to decrement, load first question) -- on click

  $("#startGame").on("click", function() {
    $("#startGame").replaceWith();

    startTimer();
    decrement();
    firstQuestion();
  });

  //this is your timer.  It is working.  Do not touch it.
  function startTimer() {
    intervalId = setInterval(decrement, 1000);
  }

  function decrement() {
    timer--;
    $("#countdown").html("<span>" + timer + "<span>");

    if (timer === 0) {
      stopTimer();
    }
  }

  function stopTimer() {
    clearInterval(intervalId);
    nextQuestion();
  }

  function firstQuestion() {
    //Get first question
    for (let i = 0; i < questionArray.length; i++) {
      $("#question-display").html(questionArray[i].question);

      //Loop through question array and create buttons for each answer
      // Clear button div of any newly created buttons
      $("#answer-display").empty();
      for (let j = 0; j < questionArray[i].answers.length; j++) {
        var a = $("<button>");

        // add class to the button
        a.addClass("btn-answer");
        // adds a data attribute
        a.data("name", questionArray[i].answers[j]);

        //label buttons
        a.text(questionArray[i].answers[j]);

        //if else statement, randomQuestion.answers is e qual to randomQuestion.correctAnswer, then add btn-correct answer

        if (questionArray[i].answers[j] == questionArray[i].correctAnswer) {
          a.addClass("btn-correctAnswer");
          a.addClass("btn-answer");
        } else {
          a.addClass("btn-answer");
        }

        $("#answer-display").append(a);
      }

      //adds button to div
      $("#answer-display").append(a);
    }
  }

  $("#answer-display").on("click", ".btn-answer", function(event) {
    //get answer from clicked element
    event.preventDefault();
    var answer = $(".btn-answer").val;
    //get correct answer from parent element
    var correctAnswer = $(this).hasClass("btn-correctAnswer");
    console.log(correctAnswer);
    //correct logic
    if (correctAnswer) {
      $("#alert-box").text("You got it, daddy-o!");
      // numberCorrect++;

      setTimeout(nextQuestion, 2000);
      stopTimer();
      // checkScore();
    } else {
      $("#alert-box").text(
        "You're cruisin' for a bruisin'- that answer is wrong!"
      );
      // numberIncorrect++;
      setTimeout(nextQuestion, 2000);
      stopTimer();
      // checkScore();
    }
  });

  function nextQuestion() {
    $("#question-display").empty();
    $("#answer-display").empty();
    $("#countdown").empty();

    timer = 11;
    startTimer();
    firstQuestion();
    console.log("nextQuestion is running!");
  }
};
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

问题出在您的nextQuestion功能中。你一直在那个函数中调用firstQuestion()。好吧......你不断得到像你一样的第一个问题,因为每次调用该函数时,你都会将i重置为0,循环从第一个问题开始。

更好的解决方案可能类似于getQuestion函数,它接受一个参数来获取问题。因此,您可以将其称为getQuestion(i),其中i是您设置的变量,并在循环外增加