Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
FermiQuestions authored Feb 8, 2024
0 parents commit ec77054
Show file tree
Hide file tree
Showing 4 changed files with 534 additions and 0 deletions.
78 changes: 78 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var currentScore = 0;
var currentQuestion = 0;
var numQuestions = 0;
var questions_and_answers = [];
var q_and_a = []

function loadData() {
fetch("data.txt")
.then((res) => res.text())
.then((text) => {
questions_and_answers = text.split('\n');
var temp = [];
for (var i = 1; i < questions_and_answers.length; i+=2) {
temp.push([questions_and_answers[i-1], questions_and_answers[i]]);
}
q_and_a = temp
.map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value)
numQuestions = q_and_a.length
document.getElementById("answeredFraction").innerText = "0/" + numQuestions.toString() + " questions answered";
setQuestion();
})
}

function getInput() {
document.getElementById("answeredFraction").innerText = Math.min(currentQuestion+1,numQuestions).toString() + "/" + numQuestions.toString() + " questions answered";
var input = document.getElementById("quantity").value;
if (currentQuestion < numQuestions) {
var correctAnswer = parseInt(q_and_a[currentQuestion][1])
if (Math.abs(input - correctAnswer) == 2) {
currentScore += 1;
document.getElementById("score").innerText = "Score: " + currentScore.toString();
document.getElementById("scoreDescription").innerText = "+1 (You guessed " + input.toString() + " and the correct answer was " + correctAnswer.toString() + ")";
document.getElementById("scoreDescription").style.color = "#DC4D01";
}
else if (Math.abs(input - correctAnswer) == 1) {
currentScore += 3;
document.getElementById("score").innerText = "Score: " + currentScore.toString();
document.getElementById("scoreDescription").innerText = "+3 (You guessed " + input.toString() + " and the correct answer was " + correctAnswer.toString() + ")";
document.getElementById("scoreDescription").style.color = "blue";
}
else if (input == correctAnswer) {
currentScore += 5;
document.getElementById("score").innerText = "Score: " + currentScore.toString();
document.getElementById("scoreDescription").innerText = "+5 (You guessed " + input.toString() + " and the correct answer was " + correctAnswer.toString() + ")";
document.getElementById("scoreDescription").style.color = "green";
}
else {
document.getElementById("score").innerText = "Score: " + currentScore.toString();
document.getElementById("scoreDescription").innerText = "+0 (You guessed " + input.toString() + " and the correct answer was " + correctAnswer.toString() + ")";
document.getElementById("scoreDescription").style.color = "red";
}
}
currentQuestion+=1;
setQuestion();
document.getElementById("quantity").value = 0;
}

function setQuestion() {
if (currentQuestion >= numQuestions) {
document.getElementById("question").innerText = "You've completed all the questions!";
document.getElementById("scoreDescription").innerText = "";
}
else {
document.getElementById("question").innerText = q_and_a[currentQuestion][0];
}
}

function keyPress(key) {
if (key.code == 'Enter') {
getInput()
}
}

window.onload = function() {
loadData();
}
Loading

0 comments on commit ec77054

Please sign in to comment.