增加计数器以访问对象的下一部分

var correctGuesses = 0; var incorrectGuesses = 0; var timeLeft = 30; var questionCounter = 0; var gameWin = false; // questions, answers, wrong guesses, true or false var quizItems = [{ question: "What swimming stroke is named after an insect?", correctAnswer: "Butterfly", incorrect1: "Bee", incorrect2: "Cricket", incorrect3: "Wasp" }, { question: "According to the San Francisco Department of Animal Care and Control, what is the most commonly reported wild animal sighted by people in the streets of San Francisco?", correctAnswer: "Raccoon", incorrect1: "Rat", incorrect2: "Possum", incorrect3: "Fox" }, { question: "What substance can you drop onto a scorpions head to make it sting itself to death?", correctAnswer: "Alcohol", incorrect1: "Cider", incorrect2: "Vinegar", incorrect3: "Bleach" }, { question: "Cows have sweat glands in what part of their bodies? ", correctAnswer: "Nose", incorrect1: "Tongue", incorrect2: "Ears", incorrect3: "Feet" } ]; $("#start-button").on("click", function() { setInterval(countdown, 1000); $("#timer").text(timeLeft); displayQuestions(); }); //Click events for all the guesses $('body').on('click', '.answer', function(){ var playerGuess = $(this).text(); if (playerGuess === quizItems[questionCounter].correctAnswer) { correctGuesses++; displayQuestions(); } else { incorrectGuesses++; displayQuestions(); } }); // Display Question 1 function displayQuestions(questionCounter) { $("#question-area").text(quizItems[questionCounter].question); $("#answer1").text(quizItems[questionCounter].incorrect1); $("#answer2").text(quizItems[questionCounter].incorrect2); $("#answer3").text(quizItems[questionCounter].incorrect3); $("#answer4").text(quizItems[questionCounter].correctAnswer); } // Countdown Timer Function function countdown() { if (timeLeft === 0) { } else { timeLeft--; $("#timer").text(timeLeft); console.log(timeLeft); } }; 

每次猜测时,我都会将questionCounter递增1。

我的想法是通过像使用questionCounter一样导航到下一个问题 – $("#answer1").text(quizItems[questionCounter].incorrect1); 当问题计数器更改数字时,它应该访问对象中的下一个问题。

我无法弄清楚为什么这不起作用。 我知道我很亲密,我确信它是一个简单的解决方案,但我被卡住了