Skip to content

Commit

Permalink
Merge pull request #57 from LeftValues/better-backbutton
Browse files Browse the repository at this point in the history
better backbutton
  • Loading branch information
sparkz9502 authored May 12, 2020
2 parents aee0eef + 7a46d2d commit 53a0e80
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 85 deletions.
2 changes: 1 addition & 1 deletion i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function getCookie (name) {
const parts = value.split('; ' + name + '=')
if (parts.length === 2) return parts.pop().split(';').shift()
}
function setCookie (cname, cvalue, exhours) {
function setCookie (cname, cvalue, exhours = 1) {
const d = new Date()
d.setTime(Date.now() + (exhours * 60 * 60 * 1000))
const expires = 'expires=' + d.toUTCString()
Expand Down
85 changes: 1 addition & 84 deletions quiz.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,90 +49,7 @@ <h2 data-i18n="quiz_loading" style="text-align:center;" id="question-number">Loa

<!-- JavaScript for the test itself -->
<script type="application/javascript" src="i18n.js"></script>
<script>
/* global i18n, loadTranslation, questions */
let maxA, maxB, maxC, maxD, maxE, maxF, maxG // Max possible scores
maxA = maxB = maxC = maxD = maxE = maxF = maxG = 0
let a, b, c, d, e, f, g // User's scores
a = b = c = d = e = f = g = 0
let qn = 0 // Question number
let prevAnswer = null

function initQuestion () {
document.getElementById('question-text').innerHTML = i18n.getString(`question_${qn}`)
document.getElementById('question-number').innerHTML = i18n.getString('quiz_question_of', {
count: qn + 1,
total: Object.size(questions)
})
if (prevAnswer == null) {
document.getElementById('back_button').style.display = 'none'
document.getElementById('back_button_off').style.display = 'block'
} else {
document.getElementById('back_button').style.display = 'block'
document.getElementById('back_button_off').style.display = 'none'
}
}

function nextQuestion (mult) { // eslint-disable-line no-unused-vars
a += mult * questions[`question_${qn}`].a
b += mult * questions[`question_${qn}`].b
c += mult * questions[`question_${qn}`].c
d += mult * questions[`question_${qn}`].d
e += mult * questions[`question_${qn}`].e
f += mult * questions[`question_${qn}`].f
g += mult * questions[`question_${qn}`].g
qn++
prevAnswer = mult
if (qn < Object.size(questions)) {
initQuestion()
} else {
results()
}
}

function prevQuestion () { // eslint-disable-line no-unused-vars
if (prevAnswer == null) {
return
}
qn--
a -= prevAnswer * questions[`question_${qn}`].a
b -= prevAnswer * questions[`question_${qn}`].b
c -= prevAnswer * questions[`question_${qn}`].c
d -= prevAnswer * questions[`question_${qn}`].d
e -= prevAnswer * questions[`question_${qn}`].e
f -= prevAnswer * questions[`question_${qn}`].f
g -= prevAnswer * questions[`question_${qn}`].g
prevAnswer = null
initQuestion()
}

function calcScore (score, max) {
return (100 * (max + score) / (2 * max)).toFixed(1)
}

function results () {
window.location.href = 'results.html' +
`?a=${calcScore(a, maxA)}` +
`&b=${calcScore(b, maxB)}` +
`&c=${calcScore(c, maxC)}` +
`&d=${calcScore(d, maxD)}` +
`&e=${calcScore(e, maxE)}` +
`&f=${calcScore(f, maxF)}` +
`&g=${calcScore(g, maxG)}`
}
loadTranslation().then(() => {
initQuestion()
for (let i = 0; i < Object.size(questions); i++) {
maxA += Math.abs(questions[`question_${i}`].a)
maxB += Math.abs(questions[`question_${i}`].b)
maxC += Math.abs(questions[`question_${i}`].c)
maxD += Math.abs(questions[`question_${i}`].d)
maxE += Math.abs(questions[`question_${i}`].e)
maxF += Math.abs(questions[`question_${i}`].f)
maxG += Math.abs(questions[`question_${i}`].g)
}
})
</script>
<script type="application/javascript" src="quiz.js"></script>
</body>

</html>
152 changes: 152 additions & 0 deletions quiz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* global i18n, loadTranslation, questions, setCookie, getCookie */
let maxA, maxB, maxC, maxD, maxE, maxF, maxG // Max possible scores
maxA = maxB = maxC = maxD = maxE = maxF = maxG = 0

const cookie = {
questionsAnswered: [],
questionNumber: 0,
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0
}

function saveStats (answer) {
if (answer !== false) {
cookie.questionsAnswered.push({
questionNumber: cookie.questionNumber,
answer
})
}
setCookie('questionsAnswered', JSON.stringify(cookie.questionsAnswered))
setCookie('questionNumber', cookie.questionNumber)
setCookie('a', cookie.a)
setCookie('b', cookie.b)
setCookie('c', cookie.c)
setCookie('d', cookie.d)
setCookie('e', cookie.e)
setCookie('f', cookie.f)
setCookie('g', cookie.g)
}

function loadStats () {
if (getCookie('questionsAnswered')) {
cookie.questionsAnswered = JSON.parse(getCookie('questionsAnswered'))
cookie.questionNumber = parseInt(getCookie('questionNumber')) || 0
cookie.a = parseInt(getCookie('a')) || 0
cookie.b = parseInt(getCookie('b')) || 0
cookie.c = parseInt(getCookie('c')) || 0
cookie.d = parseInt(getCookie('d')) || 0
cookie.e = parseInt(getCookie('e')) || 0
cookie.f = parseInt(getCookie('f')) || 0
cookie.g = parseInt(getCookie('g')) || 0
}
}

function resetStats () {
cookie.questionsAnswered = []
cookie.questionNumber = 0
cookie.a = 0
cookie.b = 0
cookie.c = 0
cookie.d = 0
cookie.e = 0
cookie.f = 0
cookie.g = 0
saveStats(false)
window.location.reload()
}

function getLastAnswer (questionNumber) {
return cookie.questionsAnswered.find(answer => answer.questionNumber === questionNumber)
}

function initQuestion () {
loadStats()
console.log(cookie)
document.getElementById('question-text').innerHTML = i18n.getString(`question_${cookie.questionNumber}`)
document.getElementById('question-number').innerHTML = i18n.getString('quiz_question_of', {
count: cookie.questionNumber + 1,
total: Object.size(questions)
})
if (cookie.questionNumber >= Object.size(questions)) {
resetStats()
}
if (getLastAnswer(cookie.questionNumber - 1) == null) {
document.getElementById('back_button').style.display = 'none'
document.getElementById('back_button_off').style.display = 'block'
} else {
document.getElementById('back_button').style.display = 'block'
document.getElementById('back_button_off').style.display = 'none'
}
}

function calculateStat (answer) {
cookie.a += answer * questions[`question_${cookie.questionNumber}`].a
cookie.b += answer * questions[`question_${cookie.questionNumber}`].b
cookie.c += answer * questions[`question_${cookie.questionNumber}`].c
cookie.d += answer * questions[`question_${cookie.questionNumber}`].d
cookie.e += answer * questions[`question_${cookie.questionNumber}`].e
cookie.f += answer * questions[`question_${cookie.questionNumber}`].f
cookie.g += answer * questions[`question_${cookie.questionNumber}`].g
}

function calculateScore (score, max) {
return (100 * (max + score) / (2 * max)).toFixed(1)
}

function nextQuestion (answer) { // eslint-disable-line no-unused-vars
calculateStat(answer)
cookie.questionNumber++
saveStats(answer)

if (cookie.questionNumber < Object.size(questions)) {
initQuestion()
} else {
resetStats()
results()
}
}

function prevQuestion () { // eslint-disable-line no-unused-vars
const previousAnswer = getLastAnswer(cookie.questionNumber - 1)
if (previousAnswer === null) {
return
}
cookie.questionsAnswered = cookie.questionsAnswered.filter(answer => {
if (answer.questionNumber !== cookie.questionNumber) {
return answer
}
})
cookie.questionNumber--

calculateStat(previousAnswer)
saveStats(previousAnswer)
initQuestion()
}

function results () {
window.location.href = 'results.html' +
`?a=${calculateScore(cookie.a, maxA)}` +
`&b=${calculateScore(cookie.b, maxB)}` +
`&c=${calculateScore(cookie.c, maxC)}` +
`&d=${calculateScore(cookie.d, maxD)}` +
`&e=${calculateScore(cookie.e, maxE)}` +
`&f=${calculateScore(cookie.f, maxF)}` +
`&g=${calculateScore(cookie.g, maxG)}`
}
loadTranslation().then(() => {
initQuestion()
for (let i = 0; i < Object.size(questions); i++) {
maxA += Math.abs(questions[`question_${i}`].a)
maxB += Math.abs(questions[`question_${i}`].b)
maxC += Math.abs(questions[`question_${i}`].c)
maxD += Math.abs(questions[`question_${i}`].d)
maxE += Math.abs(questions[`question_${i}`].e)
maxF += Math.abs(questions[`question_${i}`].f)
maxG += Math.abs(questions[`question_${i}`].g)
}
})

0 comments on commit 53a0e80

Please sign in to comment.