繁体   English   中英

javascript 中的变量值不递增

[英]variable value not incrementing in javascript

const qno = document.getElementById("qno");
const exit = document.getElementById("exit");
const qncol = document.getElementById("qncol");
const quiz = document.getElementById("quiz");
const question = document.getElementById("question");
const choice1 = document.getElementById("never");
const choice2 = document.getElementById("rarely");
const choice3 = document.getElementById("often");
const choice4 = document.getElementById("always");
const next = document.getElementById("next");
const analysis=document.getElementById("analysis")

let questions = [
    {
        question : "Having little or no interest in day-to-day actvities!",
        choice1 : "Never",
        choice2 : "Rarely",
        choice3 : "Often",
        choice4 : "Always"
    },
    {
        question : "Do you feel guilty or tearful for no reason?",
        choice1 : "Never",
        choice2 : "Rarely",
        choice3 : "Often",
        choice4 : "Always"
    },{
        question : "Trouble falling or staying asleep, due to nightmares?",
        choice1 : "Never",
        choice2 : "Rarely",
        choice3 : "Often",
        choice4 : "Always"
    },
    {
        question : "You feel depressed even when good things happen to you.",
        choice1 : "Never",
        choice2 : "Rarely",
        choice3 : "Often",
        choice4 : "Always"
    },
];


const lastQuestion = questions.length - 1;
let runningQuestion = 0;
let c1=0;
let c2=0;
let c3=0;
let c4=0;
let scorepos=0;
let scoreneg=0;

function renderQuestion(){
    let q = questions[runningQuestion];    
    question.innerHTML = "<p>"+ q.question +"</p>";
    choice1.innerHTML = q.choice1;
    choice2.innerHTML = q.choice2;
    choice3.innerHTML = q.choice3;
    choice4.innerHTML = q.choice4;
}

start.addEventListener("click",startQuiz);

function startQuiz(){
    next.style.display="block"
    exit.style.display= "block";
    qncol.style.display= "block";
    qno.style.display= "block";
    start.style.display = "none";
    quiz.style.display = "block";
    renderQuestion();

}

//next.addEventListener("click",nextQuestion);
console.log(c1);
//function nextQuestion(){}

function storeAnswer(answer){
    if( answer == questions[runningQuestion].choice1){
        c1++;
        console.log(c1);
    }else if( answer == questions[runningQuestion].choice2){
        c2++;
        console.log(c2);
    }else if( answer == questions[runningQuestion].choice3){
        c3++;
        console.log(c3);
    }else if( answer == questions[runningQuestion].choice4){
        c4++;
        console.log(c4);
    }

    scorepos=c1+c2;
    scoreneg=c3+c4;
    console.log(c1);
    if(runningQuestion < lastQuestion){
        runningQuestion++;
        return renderQuestion();
    }else{
        next.style.display="none"
        qncol.style.display= "none";
        quiz.style.display = "none";
        return graphRender(scorepos,scoreneg);
    }
}

当我运行 function 时,它不会增加 c1、c2、c3、c4 的值。 控制台显示 0 作为我的答案。 我不确定错误发生在哪里,因为当我的 html 页面执行此 javascript 文件时,执行 function 中的其余代码,除了变量的增量。 所以 js 文件有测验的问题,它呈现在 html 页面中,现在我想做一个分析,所以我引入了 4 个变量,它将存储每个选项的点击次数。 然后可用于在 html 页面上呈现图形。

点击时从 HTML 页面传递的值可能是错误的,这就是该值不递增的原因。 请检查 HTML 代码,您在其中打印选择并在“onclick”上调用 function。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM