
안녕하세요, 웹개발 종합반 수강하고, 현재 웹개발 플러스반을 듣고 있는 사람입니다. 웹개발 종합반 수강하면서, 처음 웹개발에 도전해서, 현재 http://harimath.com이라는 웹사이트를 만들었는데요.
다름이 아니라, 수업에 대한 질문 자체는 아니고, 도저히 검색을 해봐도 나오질 않아서, 질문하나만 올려봅니다. jQuery 관련한 질문이라, 여기에 올리는게 맞는듯 하여 올려봅니다..
harimath.com/schoolmath 클릭해보시면, 제가 퀴즈 애플리케이션을 하나 만들었는데, next question 말고, previous question 버튼을 만들고자,
html에다가는
<input type="button" onclick="goback()" value="Previous Question"> 이라고 써둔 다음에,
제 해당 js 코드에다가 다음과 같이 짜보았는데, 실행이 되질 않더라구요. 그런데, next question에 있던 코드로는 잘 진행이 되었는데, 그걸 반대로 decrement function으로 실행을 하려하는데, 되질 않아서, 이게 원래 이렇게 쓰면 안되는건지, 아니면 다르게 접근해야하는건지 알고 싶습니다.
next question에 해당되는 html 코드는
<input type="button" onclick="countAnswer()" value="Next Question">입니다.
js 코드 첨부합니다. decrement 기능을 제가 이해 못하고 있는건지 싶습니다. 웹개발 종합반 수강하고, 개인 프로젝트처럼 만든 웹사이트인데, 플러스반 수강하면서도, 아래 질문에 대한 답변은 잘 모르겠어서요..
// json array movement variable
var i = 0;
var correctCount = 0 ;
//initialize the first question
generate(0);
// generate from json array data with index.
function generate(index){
document.getElementById("number").innerHTML = "<b> Question "+ jsonData[index].num +".</b>"+ " (Level : " +jsonData[index].category + ")";
document.getElementById("question").innerHTML = jsonData[index].q ;
document.getElementById("optt1").innerHTML = jsonData[index].opt1 ;
document.getElementById("optt2").innerHTML = jsonData[index].opt2 ;
document.getElementById("optt3").innerHTML = jsonData[index].opt3 ;
document.getElementById("optt4").innerHTML = jsonData[index].opt4 ;
document.getElementById("optt5").innerHTML = jsonData[index].opt5 ;
}
function countAnswer(){
if(document.getElementById("opt1").checked && jsonData[i].opt1 == jsonData[i].answer){
correctCount++;
}
if(document.getElementById("opt2").checked && jsonData[i].opt2 == jsonData[i].answer){
correctCount++;
}
if(document.getElementById("opt3").checked && jsonData[i].opt3 == jsonData[i].answer){
correctCount++;
}
if(document.getElementById("opt4").checked && jsonData[i].opt3 == jsonData[i].answer){
correctCount++;
}
if(document.getElementById("opt5").checked && jsonData[i].opt3 == jsonData[i].answer){
correctCount++;
}
i++; //increment i for next question
if(jsonData.length-1<i) {
document.write("Your score is " + correctCount + " out of " + jsonData.length + ". Great work! Page will reload within seconds.");
setTimeout(function() {
window.location.reload();
}, 4000);
}
generate(i); //callback to generate
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
}
function goback(){
i--; //decrement i for the previous question.
if(i=0){
window.location.reload();
}
else{
generate(i); //callback to generate
}
}
