From bb9ec41cbdc8521ec79a9334abe32e152527f96b Mon Sep 17 00:00:00 2001 From: luca0N! Date: Sun, 30 May 2021 16:05:58 -0300 Subject: [PATCH] Calculate user score --- res/js/main.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/res/js/main.js b/res/js/main.js index 2f3683a..ee2601d 100644 --- a/res/js/main.js +++ b/res/js/main.js @@ -25,7 +25,7 @@ * by luca0N! */ -let quizEl, mappedIds = []; +let quiz, quizEl, mappedIds = []; // Attach setUpStrings() to onReady() in order to execute the main code as soon as possible. This overwrites onReady(). onReady = setUpStrings; @@ -41,7 +41,7 @@ function setUpStrings(){ warning.style['display'] = 'unset'; // Get JSON file which contains all of the challenges required. sendGetRequest('privacy-quiz.en.json', function(text){ - let quiz = JSON.parse(text); + quiz = JSON.parse(text); // Set up UI elements based on quiz metadata. let quizTitle = document.getElementById('quiz-title'); @@ -112,6 +112,24 @@ function setUpStrings(){ let finish = document.createElement('button'); finish.innerText = 'Finish!'; finish.onclick = function(){ + // Calculate the results. + let score = 0; + // Retrieve choices. + let contents = quiz.contents; + let choices = []; + for (let x = 0; x < contents.length; x++){ + choices[x] = []; + for (let y = 0; y < contents[x].choices.length; y++){ + let currentEl = document.getElementById('sec.' + x + '.ch.' + y); // current choice element + // Is it checked? + if (currentEl.checked){ + // Get the ID + //choices[x].push(mappedIds[x][y]); + score += quiz.reports[mappedIds[x][y]].points; + } + } + } + // Hide quiz and then show the results. quizEl.style['display'] = 'none'; document.getElementById('results').style['display'] = 'unset';