Skip to content

Commit

Permalink
Merge pull request #125 from Talha-ai/main
Browse files Browse the repository at this point in the history
Implemented deduction of points for wrong answer in all levels
  • Loading branch information
B1N4RY-P4R45173 authored Oct 25, 2024
2 parents e40d6a3 + 59fd9b1 commit 5c80e1c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 54 deletions.
41 changes: 22 additions & 19 deletions main/level_1/question.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ <h1 id="question">What is the answer to this question?</h1>
let score = 0;
let questionCounter = 0;
let availableQuestions = [];
let retries = 0; // Track retries for each question
let retries = 0;
let retryScore = 200;

let questions = [
{
Expand Down Expand Up @@ -125,14 +126,14 @@ <h1 id="question">What is the answer to this question?</h1>
startGame = () => {
questionCounter = 0;
score = 0;
retries = 0;
availableQuestions = [...questions];
getNewQuestion();
};

getNewQuestion = () => {
retries = 0;
hasRetried = false;
retryScore = SCORE_POINTS;
explanationElement.classList.add("hidden");

if (questionCounter >= MAX_QUESTIONS) {
localStorage.setItem("mostRecentScore", score);
Expand Down Expand Up @@ -167,16 +168,20 @@ <h1 id="question">What is the answer to this question?</h1>
selectedChoice.parentElement.classList.add(classToApply);

if (classToApply === "correct") {
incrementScore(SCORE_POINTS);
incrementScore(retryScore);
setTimeout(() => {
getNewQuestion();
}, 1000);
} else {
if (retries === 0) {
retryButton.dataset.selectedChoice = selectedAnswer;
explanationElement.classList.remove("hidden");
explanationText.innerText = currentQuestion.explanation;
retries++;
retryScore -= 100;
if (retryScore < 0) retryScore = 0;

explanationText.innerText = currentQuestion.explanation;
explanationElement.classList.remove("hidden");
retries++;

if (retries < 3) {
acceptingAnswers = true;
} else {
setTimeout(() => {
getNewQuestion();
Expand All @@ -187,18 +192,15 @@ <h1 id="question">What is the answer to this question?</h1>
});

retryQuestion = () => {
if (retries !== 1) {
return; // Retry only allowed once per question
if (retries >= 3) {
return;
}

const selectedNumber = retryButton.dataset.selectedChoice;
const selectedChoice = document.querySelector(`[data-number="${selectedNumber}"]`);

if (!selectedChoice) {
console.error("Invalid choice selected.");
return;
const selectedChoice = document.querySelector(".incorrect");
if (selectedChoice) {
selectedChoice.classList.remove("incorrect");
}
selectedChoice.parentElement.classList.remove("incorrect");

explanationElement.classList.add("hidden");
acceptingAnswers = true;
};
Expand All @@ -209,8 +211,9 @@ <h1 id="question">What is the answer to this question?</h1>
};

startGame();

</script>


</body>

</html>
36 changes: 19 additions & 17 deletions main/level_2/question.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ <h1 id="question">What is the answer to this question?</h1>
let score = 0;
let questionCounter = 0;
let availableQuestions = [];
let retries = 0; // Track retries for each question
let retries = 0;
let retryScore = 200;

let questions = [
{
Expand Down Expand Up @@ -126,13 +127,13 @@ <h1 id="question">What is the answer to this question?</h1>
startGame = () => {
questionCounter = 0;
score = 0;
retries = 0;
availableQuestions = [...questions];
getNewQuestion();
};

getNewQuestion = () => {
retries = 0;
retryScore = SCORE_POINTS;
explanationElement.classList.add("hidden");

if (questionCounter >= MAX_QUESTIONS) {
Expand Down Expand Up @@ -168,16 +169,20 @@ <h1 id="question">What is the answer to this question?</h1>
selectedChoice.parentElement.classList.add(classToApply);

if (classToApply === "correct") {
incrementScore(SCORE_POINTS);
incrementScore(retryScore);
setTimeout(() => {
getNewQuestion();
}, 1000);
} else {
if (retries === 0) {
retryButton.dataset.selectedChoice = selectedAnswer;
explanationElement.classList.remove("hidden");
explanationText.innerText = currentQuestion.explanation;
retries++;
retryScore -= 100;
if (retryScore < 0) retryScore = 0;

explanationText.innerText = currentQuestion.explanation;
explanationElement.classList.remove("hidden");
retries++;

if (retries < 3) {
acceptingAnswers = true;
} else {
setTimeout(() => {
getNewQuestion();
Expand All @@ -188,18 +193,15 @@ <h1 id="question">What is the answer to this question?</h1>
});

retryQuestion = () => {
if (retries !== 1) {
return; // Retry only allowed once per question
if (retries >= 3) {
return;
}

const selectedNumber = retryButton.dataset.selectedChoice;
const selectedChoice = document.querySelector(`[data-number="${selectedNumber}"]`);

if (!selectedChoice) {
console.error("Invalid choice selected.");
return;
const selectedChoice = document.querySelector(".incorrect");
if (selectedChoice) {
selectedChoice.classList.remove("incorrect");
}
selectedChoice.parentElement.classList.remove("incorrect");

explanationElement.classList.add("hidden");
acceptingAnswers = true;
};
Expand Down
38 changes: 20 additions & 18 deletions main/level_3/question.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ <h1 id="question">What is the answer to this question?</h1>
let score = 0;
let questionCounter = 0;
let availableQuestions = [];
let retries = 0; // Track retries for each question
let retries = 0;
let retryScore = 200;

let questions = [
{
Expand All @@ -106,7 +107,7 @@ <h1 id="question">What is the answer to this question?</h1>
choice4: "It doesn't matter; all passwords are equally secure.",
answer: 3,
explanation: "Strong passwords with combinations of letters, numbers, and symbols reduce the risk of being cracked by hackers."
},
},
{
question: "What is a passphrase?",
choice1: "A passphrase is a type of password consisting of only numbers.",
Expand Down Expand Up @@ -142,15 +143,14 @@ <h1 id="question">What is the answer to this question?</h1>
startGame = () => {
questionCounter = 0;
score = 0;
retries = 0;
availableQuestions = [...questions];
getNewQuestion();
};

getNewQuestion = () => {
retries = 0;
retryScore = SCORE_POINTS;
explanationElement.classList.add("hidden");
retryButton.classList.remove("disabled");

if (questionCounter >= MAX_QUESTIONS) {
localStorage.setItem("mostRecentScore", score);
Expand Down Expand Up @@ -185,16 +185,20 @@ <h1 id="question">What is the answer to this question?</h1>
selectedChoice.parentElement.classList.add(classToApply);

if (classToApply === "correct") {
incrementScore(SCORE_POINTS);
incrementScore(retryScore);
setTimeout(() => {
getNewQuestion();
}, 1000);
} else {
if (retries === 0) {
retryButton.dataset.selectedChoice = selectedAnswer;
explanationText.innerText = currentQuestion.explanation;
explanationElement.classList.remove("hidden");
retries++;
retryScore -= 100;
if (retryScore < 0) retryScore = 0;

explanationText.innerText = currentQuestion.explanation;
explanationElement.classList.remove("hidden");
retries++;

if (retries < 3) {
acceptingAnswers = true;
} else {
setTimeout(() => {
getNewQuestion();
Expand All @@ -205,17 +209,15 @@ <h1 id="question">What is the answer to this question?</h1>
});

retryQuestion = () => {
if (retries !== 1) return; // Retry only allowed once per question

const selectedNumber = retryButton.dataset.selectedChoice;
const selectedChoice = document.querySelector(`[data-number="${selectedNumber}"]`);

if (!selectedChoice) {
console.error("Invalid choice selected.");
if (retries >= 3) {
return;
}

selectedChoice.parentElement.classList.remove("incorrect");
const selectedChoice = document.querySelector(".incorrect");
if (selectedChoice) {
selectedChoice.classList.remove("incorrect");
}

explanationElement.classList.add("hidden");
acceptingAnswers = true;
};
Expand Down

0 comments on commit 5c80e1c

Please sign in to comment.