我为我做的测验写了一个percentageCorrect函数但是当我调用我的函数时它返回了错误的百分比。此外,我有一个秒计时器,但我无法弄清楚如何每60秒增加分钟。我尝试使用if语句,for循环等。下面是我的Jquery代码。请帮忙,谢谢!
var minutes = 0;
var seconds = 0;
var intervalId;
var correct = 0;
var incorrect = 0;
var timeSpent;
$(document).ready(function(){
$("#results").addClass('hide');
$('#questions').addClass('hide');
$('#startTrivia').on('click', function(){
$('#questions').removeClass('hide');
startTimer();
});
$("#submit").on('click', function(){
$(".jumbotron").addClass('hide');
$("#questions").addClass('hide');
//get the number of correct... :checked is what the user checked
var numberCorrect = $('input[data-correct="correct"]:checked').length;
var numberIncorrect = 10 - numberCorrect;
//puts in the html what questions were correct vs incorrect
$('#correct').html(numberCorrect);
$("#incorrect").html(numberIncorrect);
timeSpent = "It took you " + minutes + " minutes and " + seconds + " seconds to complete the Thanksgiving Trivia!"
$("#timeTaken").html(timeSpent);
//calculating percentage of correct answers
$("#totalPercent").html("You got " + percentCorrect(numberCorrect, numberIncorrect) + "% on the test!");
//results will show after pushing submit
$("#results").removeClass('hide');
});
//timer pauses after clicking pause timer
$("#pauseGame").on('click', function(){
pauseTimer();
$("#questions").addClass('hide');
});
//timer resets after clicking reset timer
$('#restart').on('click', function(){
resetGame();
$("#questions").addClass('hide');
$("#results").addClass('hide');
$(".jumbotron").removeClass('hide');
//clearing the checked radio buttons
$("input:checked").removeAttr("checked");
});
});
// NOT GIVING CORRECT PERCENTAGE?
function percentCorrect(x, y) {
return Math.round((x/y)*100);
};
// CAN'T FIGURE OUT HOW TO MAKE MINUTES INCREASE EVERY 60 SECONDS?
function totalTime() {
seconds++;
$('#seconds').html(seconds);
};
function pauseTimer() {
clearInterval(intervalId);
};
function startTimer() {
intervalId = setInterval(totalTime, 1000);
};
function resetGame() {
seconds = -1;
totalTime();
this.clearInterval(intervalId);
};
答案 0 :(得分:0)
您似乎错误地计算了自己的百分比。它应该是:
percentageCorrect = (totalCorrect / totalQuestions) * 100
另外,为了计算分钟数:
if(seconds % 60 == 0) minutes++;