我一整天都在为这个问题苦苦挣扎。我需要调用函数 ScoreGrade 才能获得分数。你能告诉我我需要纠正什么吗?我尝试使用这段代码,但我无法弄清楚其余的内容。 :/
function endQuiz() {
var grade = scoreGrade();
if (myAnswers[(lengthofobject-1)] {
var output = "<div class='output'>Резултат<br>";
var questionResult = "NA";
//console.log('Quiz Over');
for (var i = 0; i < myAnswers.length; i++ || "ten-countdown"<1)) {
if (data.quizcontent[i].correct == myAnswers[i]) {
questionResult = '<span class="glyphicon glyphicon-ok-circle" aria-hidden="true"></span>';
correct++;
} else {
questionResult = '<span class="glyphicon glyphicon-remove-circle" aria-hidden="true"></span>';
}
output = output + '<p>Питање ' + (i + 1) + ' ' + questionResult + '</p> ';
}
var grade = scoreGrade(correct, lengthofobject)
document.getElementById("ocena").innerHTML = grade;
function scoreGrade(){
let score = (correct / lengthofobject) * 100;
let grade;
if (score>=85) {grade ='5'}
else if (score>=70) {grade ='4'}
else if (score>=55) {grade ='3'}
else if (score>=40) {grade ='2'}
else {grade ='1'}
return grade;
}
clearInterval(endTime);
output = output + '<p>Имате ' + correct + ' од ' + lengthofobject + ' тачних одговора.</p></div> ';
document.getElementById("quizContent").innerHTML = output;
} else {
//console.log('not answered');
}
最佳答案
我猜您的问题出在 scoreGrade
函数内部,该函数不返回任何内容。让我们像这样修改它:
function scoreGrade(correct, questions.length) {
let score = (correct/questions.length) * 100;
if (score>=85) { return (<h4>Vaša ocena je 5.</h4>); }
else if (score>=70) { return (<h4>Vaša ocena je 4.</h4>); }
else if (score>=55) { return (<h4>Vaša ocena je 3.</h4>); }
else if (score>=40) { return (<h4>Vaša ocena je 2.</h4>); }
else { return (<h4>Vaša ocena je 1.</h4>); }
}
这个函数可以工作,但不适用于普通js。在js中返回html是jsx的做法。如果您不使用 jsx,请考虑仅以字符串或整数形式返回成绩值。
关于javascript - 从分数转换为等级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61259893/