Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-100 points for every wrong answer #13

Merged
merged 5 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 79 additions & 67 deletions How-Well-Do-You-Know-Kendall/game/game.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const question = document.querySelector("#question");
const choices = Array.from(document.querySelectorAll(".choice-text"));
const progressText = document.querySelector("#progressText");
const scoreText = document.querySelector("#score");
const progressBarFull = document.querySelector("#progressBarFull");
const question = document.querySelector('#question');
const choices = Array.from(document.querySelectorAll('.choice-text'));
const progressText = document.querySelector('#progressText');
const scoreText = document.querySelector('#score');
const progressBarFull = document.querySelector('#progressBarFull');

let currentQuestion = {};
let acceptingAnswers = true;
Expand All @@ -11,94 +11,106 @@ let questionCounter = 0;
let availableQuestions = [];

let questions = [
{
question: 'What is Kendall"s favorite color?',
choice1: "Blue",
choice2: "Red",
choice3: "Green",
choice4: "Yellow",
answer: 3,
},
{
question: "Since when did Kendall begin his coding journey?",
choice1: "2017",
choice2: "2018",
choice3: "2019",
choice4: "2020",
answer: 4,
},
{
question: 'Which day is Kendall"s birthday?',
choice1: "15th July",
choice2: "2nd May",
choice3: "18th November",
choice4: "10th May",
answer: 2,
},
{
question: 'What is Kendall"s favorite color?',
choice1: 'Blue',
choice2: 'Red',
choice3: 'Green',
choice4: 'Yellow',
answer: 3,
},
{
question: 'Since when did Kendall begin his coding journey?',
choice1: '2017',
choice2: '2018',
choice3: '2019',
choice4: '2020',
answer: 4,
},
{
question: 'Which day is Kendall"s birthday?',
choice1: '15th July',
choice2: '2nd May',
choice3: '18th November',
choice4: '10th May',
answer: 2,
},
];

const SCORE_POINTS = 100;
const MAX_QUESTIONS = 3;

startGame = () => {
questionCounter = 0;
score = 0;
availableQuestions = [...questions];
getNewQuestion();
questionCounter = 0;
score = 0;
availableQuestions = [...questions];
getNewQuestion();
};

getNewQuestion = () => {
if (availableQuestions.length === 0 || questionCounter > MAX_QUESTIONS) {
localStorage.setItem("mostRecentScore", score);
if (availableQuestions.length === 0 || questionCounter > MAX_QUESTIONS) {
localStorage.setItem('mostRecentScore', score);

return window.location.assign("/how-well-do-you-know-kendall/end");
}
return window.location.assign('/how-well-do-you-know-kendall/end');
}

questionCounter++;
progressText.innerText = `Question ${questionCounter} of ${MAX_QUESTIONS}`;
progressBarFull.style.width = `${(questionCounter / MAX_QUESTIONS) * 100}%`;
questionCounter++;
progressText.innerText = `Question ${questionCounter} of ${MAX_QUESTIONS}`;
progressBarFull.style.width = `${(questionCounter / MAX_QUESTIONS) * 100}%`;

const questionsIndex = Math.floor(Math.random() * availableQuestions.length);
currentQuestion = availableQuestions[questionsIndex];
question.innerText = currentQuestion.question;
const questionsIndex = Math.floor(
Math.random() * availableQuestions.length
);
currentQuestion = availableQuestions[questionsIndex];
question.innerText = currentQuestion.question;

choices.forEach((choice) => {
const number = choice.dataset["number"];
choice.innerText = currentQuestion["choice" + number];
});
choices.forEach((choice) => {
const number = choice.dataset['number'];
choice.innerText = currentQuestion['choice' + number];
});

availableQuestions.splice(questionsIndex, 1);
availableQuestions.splice(questionsIndex, 1);

acceptingAnswers = true;
acceptingAnswers = true;
};

choices.forEach((choice) => {
choice.addEventListener("click", (e) => {
if (!acceptingAnswers) return;
choice.addEventListener('click', (e) => {
if (!acceptingAnswers) return;

acceptingAnswers = false;
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset["number"];
acceptingAnswers = false;
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset['number'];

let classToApply =
selectedAnswer == currentQuestion.answer ? "correct" : "incorrect";
let classToApply =
selectedAnswer == currentQuestion.answer ? 'correct' : 'incorrect';

if (classToApply === "correct") {
incrementScore(SCORE_POINTS);
}
if (classToApply === 'correct') {
incrementScore(SCORE_POINTS);
}

selectedChoice.parentElement.classList.add(classToApply);
if (classToApply === 'incorrect') {
decrementScore(SCORE_POINTS);
}

setTimeout(() => {
selectedChoice.parentElement.classList.remove(classToApply);
getNewQuestion();
}, 1000);
});
selectedChoice.parentElement.classList.add(classToApply);

setTimeout(() => {
selectedChoice.parentElement.classList.remove(classToApply);
getNewQuestion();
}, 1000);
});
});

incrementScore = (num) => {
score += num;
scoreText.innerText = score;
score += num;
scoreText.innerText = score;
};

// function to deduct 100 points for wrong answer
decrementScore = (num) => {
score -= num;
scoreText.innerText = score;
};

startGame();
146 changes: 79 additions & 67 deletions browsers/game/game.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const question = document.querySelector("#question");
const choices = Array.from(document.querySelectorAll(".choice-text"));
const progressText = document.querySelector("#progressText");
const scoreText = document.querySelector("#score");
const progressBarFull = document.querySelector("#progressBarFull");
const question = document.querySelector('#question');
const choices = Array.from(document.querySelectorAll('.choice-text'));
const progressText = document.querySelector('#progressText');
const scoreText = document.querySelector('#score');
const progressBarFull = document.querySelector('#progressBarFull');

let currentQuestion = {};
let acceptingAnswers = true;
Expand All @@ -11,94 +11,106 @@ let questionCounter = 0;
let availableQuestions = [];

let questions = [
{
question: "Which year was the first web browser created in?",
choice1: "1985",
choice2: "1993",
choice3: "1990",
choice4: "1991",
answer: 3,
},
{
question: "Netscape Browser Version 8.0 was released in?",
choice1: "May 2005",
choice2: "April 2005",
choice3: "July 2004",
choice4: "December 2007",
answer: 1,
},
{
question: "What was the first web browser?",
choice1: "Netscape",
choice2: "Mozilla Firefox",
choice3: "IE (Internet Explorer)",
choice4: "World Wide Web",
answer: 4,
},
{
question: 'Which year was the first web browser created in?',
choice1: '1985',
choice2: '1993',
choice3: '1990',
choice4: '1991',
answer: 3,
},
{
question: 'Netscape Browser Version 8.0 was released in?',
choice1: 'May 2005',
choice2: 'April 2005',
choice3: 'July 2004',
choice4: 'December 2007',
answer: 1,
},
{
question: 'What was the first web browser?',
choice1: 'Netscape',
choice2: 'Mozilla Firefox',
choice3: 'IE (Internet Explorer)',
choice4: 'World Wide Web',
answer: 4,
},
];

const SCORE_POINTS = 100;
const MAX_QUESTIONS = 3;

startGame = () => {
questionCounter = 0;
score = 0;
availableQuestions = [...questions];
getNewQuestion();
questionCounter = 0;
score = 0;
availableQuestions = [...questions];
getNewQuestion();
};

getNewQuestion = () => {
if (availableQuestions.length === 0 || questionCounter > MAX_QUESTIONS) {
localStorage.setItem("mostRecentScore", score);
if (availableQuestions.length === 0 || questionCounter > MAX_QUESTIONS) {
localStorage.setItem('mostRecentScore', score);

return window.location.assign("/browsers/end");
}
return window.location.assign('/browsers/end');
}

questionCounter++;
progressText.innerText = `Question ${questionCounter} of ${MAX_QUESTIONS}`;
progressBarFull.style.width = `${(questionCounter / MAX_QUESTIONS) * 100}%`;
questionCounter++;
progressText.innerText = `Question ${questionCounter} of ${MAX_QUESTIONS}`;
progressBarFull.style.width = `${(questionCounter / MAX_QUESTIONS) * 100}%`;

const questionsIndex = Math.floor(Math.random() * availableQuestions.length);
currentQuestion = availableQuestions[questionsIndex];
question.innerText = currentQuestion.question;
const questionsIndex = Math.floor(
Math.random() * availableQuestions.length
);
currentQuestion = availableQuestions[questionsIndex];
question.innerText = currentQuestion.question;

choices.forEach((choice) => {
const number = choice.dataset["number"];
choice.innerText = currentQuestion["choice" + number];
});
choices.forEach((choice) => {
const number = choice.dataset['number'];
choice.innerText = currentQuestion['choice' + number];
});

availableQuestions.splice(questionsIndex, 1);
availableQuestions.splice(questionsIndex, 1);

acceptingAnswers = true;
acceptingAnswers = true;
};

choices.forEach((choice) => {
choice.addEventListener("click", (e) => {
if (!acceptingAnswers) return;
choice.addEventListener('click', (e) => {
if (!acceptingAnswers) return;

acceptingAnswers = false;
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset["number"];
acceptingAnswers = false;
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset['number'];

let classToApply =
selectedAnswer == currentQuestion.answer ? "correct" : "incorrect";
let classToApply =
selectedAnswer == currentQuestion.answer ? 'correct' : 'incorrect';

if (classToApply === "correct") {
incrementScore(SCORE_POINTS);
}
if (classToApply === 'correct') {
incrementScore(SCORE_POINTS);
}

selectedChoice.parentElement.classList.add(classToApply);
if (classToApply === 'incorrect') {
decrementScore(SCORE_POINTS);
}

setTimeout(() => {
selectedChoice.parentElement.classList.remove(classToApply);
getNewQuestion();
}, 1000);
});
selectedChoice.parentElement.classList.add(classToApply);

setTimeout(() => {
selectedChoice.parentElement.classList.remove(classToApply);
getNewQuestion();
}, 1000);
});
});

incrementScore = (num) => {
score += num;
scoreText.innerText = score;
score += num;
scoreText.innerText = score;
};

// function to deduct 100 points for wrong answer
decrementScore = (num) => {
score -= num;
scoreText.innerText = score;
};

startGame();
Loading