$(document).ready(function() {
$("#remaining-time").hide();
$("#start").on('click', trivia.startGame);
$(document).on('click', '.option', trivia.guessChecker);
})
var trivia = {
correct: 0,
incorrect: 0,
unanswered: 0,
points: 0,
currentSet: 0,
timer: 20,
timerOn: false,
timerId: '',
questions: {
q1: "According to Greek mythology, who stole fire for mankind's benefit?",
q2: "Name the Chinese game played with small tiles.",
q3: "In Japanese, what is the word for goodbye?",
q4: "What nationality of soldiers wear a white kilt?",
q5: "Leonardo da Vinci was born in what country?",
q6: "Mount Fuji is the highest mountain in what conutry?",
q7: "In terms of land area, what is the largest country in the world?",
q8: "Adidas and Volkswagen are companies from what country?",
q9: "The Channel Tunnel links England with which European country?",
q10: "Adolf Hitler was born in what country?",
q11: "Portugal is bordered by only what country?",
q12: "India Ink was developed in what country?",
q13: "Which fictional city is the home of Batman?",
q14: "In which sport would you perform the Fosbury Flop?",
q15: "Spinach is high in which mineral?"
},
options: {
q1: ["Prometheus", "Hercules", "Zeus", "Odysseus"],
q2: ["Go", "Sudoku", "Mah-Jong", "Pai Gow"],
q3: ["Arigato", "Sayonara", "Konnichiwa", "Domo"],
q4: ["Scottish", "Greek", "French", "German"],
q5: ["Italy", "France", "Greece", "Great Britian"],
q6: ["Arigato", "Sayonara", "Konnichiwa", "Domo"],
q7: ["United States", "China", "Australia", "Russia"],
q8: ["Russia", "Italy", "Germany", "Mexico"],
q9: ["France", "Greece", "Italy", "Spain"],
q10: ["Germany", "Austria", "Russia", "Switzerland"],
q11: ["Austria", "Sweden", "China", "Spain"],
q12: ["China", "India", "Vietnam", "Italy"],
q13: ["Gotham", "New York", "Brooklyn", "Miami"],
q14: ["Long Jump", "High Jump", "Soccer", "Football"],
q15: ["Coal", "Gold", "Bronze", "Iron"]
},
answers: {
q1: "Prometheus",
q2: "Mah-Jong",
q3: "Sayonara",
q4: "Greek",
q5: "Italy",
q6: "Sayonara",
q7: "Russia",
q8: "Germany",
q9: "France",
q10: "Austria",
q11: "Spain",
q12: "China",
q13: "Gotham",
q14: "High Jump",
q15: "Iron"
},
startGame: function() {
trivia.currentSet = 0;
trivia.correct = 0;
trivia.incorrect = 0;
trivia.unanswered = 0;
trivia.points = 0;
clearInterval(trivia.timerId);
$('#game').show();
$('#results').html('');
$('#timer').text(trivia.timer);
$('#start').hide();
$('#remaining-time').show();
trivia.nextQuestion();
},
nextQuestion: function() {
trivia.timer = 10;
$('#timer').removeClass('last-seconds');
$('#timer').text(trivia.timer);
if (!trivia.timerOn) {
trivia.timerId = setInterval(trivia.timerRunning, 1000);
}
var questionContent = Object.values(trivia.questions)[trivia.currentSet];
$('#question').text(questionContent);
var questionOptions = Object.values(trivia.options)[trivia.currentSet];
$.each(questionOptions, function(index, key) {
$('#options').append($('<button class="option btn btn-info btn-lg">' + key + '</button>'));
})
},
timerRunning: function() {
if (trivia.timer > -1 && trivia.currentSet < Object.keys(trivia.questions).length) {
$('#timer').text(trivia.timer);
trivia.timer--;
if (trivia.timer === 4) {
$('#timer').addClass('last-seconds');
}
} else if (trivia.timer === -1) {
trivia.unanswered++;
trivia.result = false;
clearInterval(trivia.timerId);
resultId = setTimeout(trivia.guessResult, 1000);
$('#results').html('<h3>Out of time! The answer was ' + Object.values(trivia.answers)[trivia.currentSet] + '</h3>');
} else if (trivia.currentSet === Object.keys(trivia.questions).length) {
$('#results')
.html('<h3>Thank you for playing!</h3>' +
'<p>Correct: ' + trivia.correct + '</p>' +
'<p>Incorrect: ' + trivia.incorrect + '</p>' +
'<p>Unaswered: ' + trivia.unanswered + '</p>' +
'<h3>Score: ' + trivia.points + '</h3>');
$('#game').hide();
$('#start').show();
}
},
guessChecker: function() {
var resultId;
var currentAnswer = Object.values(trivia.answers)[trivia.currentSet];
if ($(this).text() === currentAnswer) {
$(this).addClass('btn-success').removeClass('btn-info');
trivia.correct++;
clearInterval(trivia.timerId);
resultId = setTimeout(trivia.guessResult, 1000);
$('#results').html('<h3>Correct Answer!</h3>');
} else {
$(this).addClass('btn-danger').removeClass('btn-info');
trivia.incorrect++;
clearInterval(trivia.timerId);
resultId = setTimeout(trivia.guessResult, 1000);
$('#results').html('<h3>Incorrect! The correct answer is: ' + currentAnswer + '</h3>');
}
},
updateScore: function() {
if ($(this).text() === currentAnswer) {
$(this).addClass('btn-success').removeClass('btn-info');
trivia.points++;
trivia.points * 100;
}
},
guessResult: function() {
trivia.currentSet++;
$('.option').remove();
$('#results h3').remove();
trivia.nextQuestion();
}
}
当前试图弄清楚为什么我的分数功能不起作用。它只是想在游戏结束时更新分数,就像它更新正确答案和错误答案的数量一样。除非分数是100倍,但是他们得到了许多正确答案。除要点外,其他所有操作均有效。谁能帮我指出我可能错过的小细节吗?
答案 0 :(得分:0)
您没有使用表达式trivia.points * 100;
中的值。您实际上需要使用赋值运算符来赋值:
trivia.points = trivia.points * 100;
或更简洁地说:
trivia.points *= 100;
尽管如此,对每个正确答案都可能不是您想要的。