javascript - 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);
}
}
当我运行该函数时,它不会增加 c1、c2、c3、c4 的值。控制台显示 0 作为我的答案。我不确定错误发生在哪里,因为当我的 html 页面执行这个 javascript 文件时,除了变量的增量之外,函数中的其余代码都会被执行。所以 js 文件有测验的问题,它呈现在 html 页面中,现在我想做一个分析,所以我引入了 4 个变量,它们将存储每个选项的点击次数。然后可用于在 html 页面上呈现图形。
解决方案
点击时从 HTML 页面传递的值可能是错误的,这就是该值不递增的原因。请检查您打印选项并在“onclick”上调用该函数的 HTML 代码。
推荐阅读
- octopus-deploy - Octopus Deploy:引发了“System.OutOfMemoryException”类型的异常
- python - 如何知道 start_text_translation_job 何时完成及其状态
- c# - 如何从 ReadOnlySpan 获取字节 []
不调用 .ToArray? - javascript - React-Router-Dom Link 改变了我的按钮的位置
- r - 如何从绘图图中读取坐标值?
- php - 如何在laravel中显示当前登录用户的数据
- mysql - 向 SQL 查询添加 JOIN 会产生不同的结果,这不是偶然的 CROSS JOIN
- r - 按标签拆分文本并设置列名
- android - Docker:工作区目录android上不允许操作
- javascript - javascript中的文档是什么?