如何在函数调用时访问数组中的对象

时间:2019-05-28 11:30:57

标签: javascript html

我创建了一个具有不同属性的对象数组,但是在将对象的每个属性显示在网页上时遇到了问题。我已经尝试过,但不知道问题出在哪里。

我尝试单独访问对象,但仍然无法正常工作,请检查问题

//Get the id's of all elements
const intro = document.getElementById("introduction");
const continued = document.getElementById("continue");
const name = document.getElementById("name").value;
const wel = document.getElementById("wel")
const startt = document.getElementById("startt");
const start = document.getElementById("start");
const quiz = document.getElementById("quiz");
const question = document.getElementById("question");
const qImg = document.getElementById("qImg");
const choiceA = document.getElementById("A");
const choiceB = document.getElementById("B");
const choiceC = document.getElementById("C");
const choiceD = document.getElementById("D");
const counter = document.getElementById("counter");
const timeGauge = document.getElementById("timeGauge");
const progress = document.getElementById("progress");
const scoreDiv = document.getElementById("scoreContainer");

//Event listeners
continued.addEventListener('click', continueAfterIntro);
start.addEventListener('click', startQuiz);

//variable declarations
const lastQuestion = questions.length - 1;
var runningQuestion = 0;
var secs = 0;
var mins = 0;
var hrs = 0;
const questionTime = 60; // 60s
const gaugeWidth = 180; // 180px
const gaugeUnit = gaugeWidth / questionTime;
let TIMER;
let score = 0;

//Array object declaration
let questions = [{
  question: "Who is the president Nigeria?",
  choiceA: "Muhamadu Buhari",
  choiceB: "Osibajo",
  choiceC: "Obasanjo",
  choiceD: "Green,Red,Green",
  correct: "A"
}, {
  question: "Who is the governor of oyo state?",
  choiceA: "Abiola Ajumobi",
  choiceB: "Seyi makinde",
  choiceC: "Alao Akala",
  choiceD: "Green,Red,Green",
  correct: "B"
}, {
  question: "What are the colors of the Nigerian Flag?",
  choiceA: "Green,White,Blue",
  choiceB: "Blue,White,Green",
  choiceC: "Green,White,Green",
  choiceD: "Green,Red,Green",
  correct: "C"
}];

function continueAfterIntro() {
  intro.style.display = 'none';
  startt.style.display = 'block';
  wel.innerHTML = `Hi ${name}`;
}

function renderQuestion() {
  let q = questions[runningQuestion];
  question.innerHTML = "<p>" + q.question + "</p>";
  choiceA.innerHTML = q.choiceA;
  choiceB.innerHTML = q.choiceB;
  choiceC.innerHTML = q.choiceC;
}

function startQuiz() {
  startt.style.display = "none";
  quiz.style.display = "block";
  renderQuestion();
}
<div id="container">
  <div class="" id="introduction">
    <div id="pimg"><img src="pic/thumbs.png" alt="WELCOME FACE"></div>
    <div id="para1">
      <p>Hey there i'm AFO by name whats yours</p>
    </div>
    <div id="name-button">
      <span id="iName"><input type="text" id="name" placeholder="Type Your Name Here"></span>
      <span id="continue"><input type="button" value="Continue" id="continue"></span>
    </div>
  </div>
  <div id="startt" style="display: none">
    <p id="wel"></p>
    <div id="start">Start Quiz!</div>
  </div>
  <div id="quiz" style="display: none">
    <div id="question"></div>
    <div id="choices">
      A.
      <div class="choice" id="A" onclick="checkAnswer('A')"></div>
      B.
      <div class="choice" id="B" onclick="checkAnswer('B')"></div>
      C.
      <div class="choice" id="C" onclick="checkAnswer('C')"></div>
      D.
      <div class="choice" id="D" onclick="checkAnswer('D')"></div>
    </div>
    <div id="timer">
      <div id="counter"></div>
      <div id="btimeGauge"></div>
      <div id="timeGauge"></div>
    </div>
    <div id="progress"></div>
  </div>
  <div id="scoreContainer" style="display: none"></div>
</div>

函数renderQuestion应该在网页上显示问题和选择

1 个答案:

答案 0 :(得分:1)

当我运行您的项目时,我得到了ReferenceError

Uncaught ReferenceError: Cannot access 'questions' before initialization

这意味着您无法在初始化之前使用Questions Array。例如:

const questionsLength = questions.length;  // REFERENCE ERROR.
const questions = ['a', 'b', 'c'];
console.log(questionsLength);

在检查长度之前声明Questions Array

const questions = ['a', 'b', 'c'];
const questionsLength = questions.length;
console.log(questionsLenght) // Returns 3

工作示例:

//Get the id's of all elements
const intro = document.getElementById("introduction");
const continued = document.getElementById("continue");
const name = document.getElementById("name").value;
const wel = document.getElementById("wel")
const startt = document.getElementById("startt");
const start = document.getElementById("start");
const quiz = document.getElementById("quiz");
const question = document.getElementById("question");
const qImg = document.getElementById("qImg");
const choiceA = document.getElementById("A");
const choiceB = document.getElementById("B");
const choiceC = document.getElementById("C");
const choiceD = document.getElementById("D");
const counter = document.getElementById("counter");
const timeGauge = document.getElementById("timeGauge");
const progress = document.getElementById("progress");
const scoreDiv = document.getElementById("scoreContainer");

//Event listeners
continued.addEventListener('click',continueAfterIntro);
start.addEventListener('click',startQuiz);


//Array object declaration
let questions = [
    {
        question : "Who is the president Nigeria?",
        choiceA : "Muhamadu Buhari",
        choiceB : "Osibajo",
        choiceC : "Obasanjo",
        choiceD : "Green,Red,Green",
        correct : "A"
    },{
        question : "Who is the governor of oyo state?",
        choiceA : "Abiola Ajumobi",
        choiceB : "Seyi makinde",
        choiceC : "Alao Akala",
        choiceD : "Green,Red,Green",
        correct : "B"
    },{
        question : "What are the colors of the Nigerian Flag?",
        choiceA : "Green,White,Blue",
        choiceB : "Blue,White,Green",
        choiceC : "Green,White,Green",
        choiceD : "Green,Red,Green",
        correct : "C"
    }
];
//variable declarations
const lastQuestion = questions.length - 1;
var runningQuestion = 0;
var secs = 0;
var mins = 0;
var hrs = 0;
const questionTime = 60; // 60s
const gaugeWidth = 180; // 180px
const gaugeUnit = gaugeWidth / questionTime;
let TIMER;
let score = 0;

function continueAfterIntro(){
    intro.style.display = 'none';
    startt.style.display = 'block';
    wel.innerHTML = `Hi ${name}`;
}

function renderQuestion(){
    let q = questions[runningQuestion];
    question.innerHTML = "<p>"+ q.question +"</p>";
    choiceA.innerHTML = q.choiceA;
    choiceB.innerHTML = q.choiceB;
    choiceC.innerHTML = q.choiceC;
}

function startQuiz(){
    startt.style.display = "none";
    quiz.style.display = "block";
    renderQuestion();
}
<!DOCTYPE html>
<html lang="en">
<head>
        <link rel="stylesheet" href="quiz.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
        <div id="container">
                    <div class="" id="introduction">
                        <div id="pimg"><img src="pic/thumbs.png" alt="WELCOME FACE"></div>
                        <div id="para1"><p>Hey there i'm AFO by name whats yours</p> </div>
                        <div id="name-button">
                        <span id="iName"><input type="text" id="name" placeholder="Type Your Name Here"></span>
                        <span id="continue"><input type="button" value="Continue" id="continue"></span>
                    </div>
                    </div>
                    <div id="startt" style="display: none">
                        <p id="wel"></p>
                <div id="start" >Start Quiz!</div>
            </div>
                <div id="quiz" style="display: none">
                    <div id="question"></div>
                    <div id="choices">
                       A.<div class="choice" id="A" onclick="checkAnswer('A')"></div>
                       B.<div class="choice" id="B" onclick="checkAnswer('B')"></div>
                       C.<div class="choice" id="C" onclick="checkAnswer('C')"></div>
                       D.<div class="choice" id="D" onclick="checkAnswer('D')"></div>
                    </div>
                    <div id="timer">
                            <div id="counter"></div>
                            <div id="btimeGauge"></div>
                            <div id="timeGauge"></div>
                        </div>
                    <div id="progress"></div>
                </div>
                <div id="scoreContainer" style="display: none"></div>
            </div>
</body>
</html>

ReferenceError意味着MDN#ReferenceError