2017-01-03 12 views
1

동영상의 지침에 따라 퀴즈 앱을 만들어야합니다. 지침을 따라 앱을 만듭니다. 거의 모든 것이 작동하지만 퀴즈가 끝나면 점수를 얻지 못합니다.TypeError : q가 정의되지 않았습니다 (JS에서)

var currentQuestion = 0; 
var score = 0; 
var totQuestions = questions.length; 

var container = document.getElementById('quizContainer'); 
var questionEl = document.getElementById('question'); 
var opt1 = document.getElementById('opt1'); 
var opt2 = document.getElementById('opt2'); 
var opt3 = document.getElementById('opt3'); 
var opt4 = document.getElementById('opt4'); 
var nextButton = document.getElementById('nextButton'); 
var resultCont = document.getElementById('result'); 

function loadQuestion (questionIndex) { 
var q = questions[questionIndex]; 
**questionEl.textContent = (questionIndex + 1) + '. ' + q.question;** 
opt1.textContent = q.option1; 
opt2.textContent = q.option2; 
opt3.textContent = q.option3; 
opt4.textContent = q.option4; 

}; 
function loadNextQuestion() { 
var selectedOption = document.querySelector('input[type=radio]:checked'); 
if(!selectedOption){ 
    alert('Please select your answer!'); 
    return; 
} 
var answer = selectedOption.value; 
if(questions[currentQuestion].answer ==answer){ 
    score += 10; 
} 
selectedOption.checked = false; 
currentQuestion++; 
if(currentQuestion == totQuestions - 1){ 
    nextButton.textContent = 'Finish'; 
} 
if(currentQuestion == totQuestions){ 
    container.style.display = 'none'; 
    resultCont.style.display = ''; 
    resultCont.textContent = 'Your score: ' + score; 
} 
loadQuestion(currentQuestion); 
} 
loadQuestion(currentQuestion); 

사람이 오류를 지적 시겠어요 : 웹 콘솔 코드 (굵은 글꼴의 16 라인)의 다음과 같은 부분에 형식 오류를 보여줍니다?

var questions = [{ 
"question": "Why do we use the present simple tense?", 
"option1": "General truths and facts", 
"option2": "Complete action", 
"option3": "Continuous action", 
"option4": "Continuous action linked with past", 
"answer": "1" 
}, { 
"question": "Why do we use the present continuous tense?", 
"option1": "General truths and facts", 
"option2": "Complete action", 
"option3": "Continuous action", 
"option4": "Continuous action linked with past", 
"answer": "3" 
}, { 
"question": "Why do we use the present perfect tense?", 
"option1": "General truths and facts", 
"option2": "Complete action", 
"option3": "Continuous action", 
"option4": "Continuous action linked with past", 
"answer": "2" 
}, { 
"question": "Why do we use the present perfect continuous tense?", 
"option1": "General truths and facts", 
"option2": "Complete action", 
"option3": "Continuous action", 
"option4": "Continuous action linked with past", 
"answer": "4" 
}] 
+0

'질문'은 무엇이며 어디에서 왔습니까? – Craicerjack

+1

'questions '란 무엇입니까? 함수에 무엇을 전달하고 있습니까? – Li357

+0

@Craicerjack 내 질문을 보류하거나 닫는 것을 피하기 위해 코드의 오류 부분 만 게시했습니다. 전체 코드를 게시해야합니까? –

답변

0

바와 같이 myvar에 값을 가지고 당신을 보장하지 않습니다 var myvar = some.thing을하고, JS의 주석에 말했다 : 여기 질문 정의되어있는 파일입니다. some 변수가 thing 속성을 포함하지 않는 경우

, 다음 some.thingundefined, 그래서 myvar 될 것입니다.

간단한 데모 here (왼쪽 하단에 콘솔 버튼 사용). 마지막 줄은 브라우저 콘솔에 오류를 발생 시키므로 수행 할 수 없으므로 undefined.someProperty입니다.

관련 문제