Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Show Correct Answer when wrong answer selected#11

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
donypsaju wants to merge22 commits intodevelop
base:develop
Choose a base branch
Loading
frommaster
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
22 commits
Select commitHold shift + click to select a range
f9f9b15
Adding a Youtube Playlist
thedamianFeb 18, 2019
df9ea21
Video youtube Playlist of James' amazing videos
thedamianFeb 18, 2019
12c2394
Updated ReadMe
jamesqquickJan 23, 2020
fa9af0b
Updated Read Me again
jamesqquickJan 23, 2020
acb701e
Got rid of exclamation
jamesqquickJan 23, 2020
c0cb8eb
Merge branch 'master' of https://github.com/jamesqquick/Build-A-Quiz-…
jamesqquickJan 23, 2020
92a5b3d
Merge pull request #1 from bocajs:master
jamesqquickApr 13, 2020
ae4c09a
Fixed minor issues.
krishna16sharmaApr 14, 2020
afdf52f
Merge pull request #4 from krishna16sharma/new_branch
jamesqquickApr 25, 2020
c409aeb
Revert "Fixed minor issues."
jamesqquickApr 25, 2020
91c5022
Merge pull request #5 from jamesqquick/revert-4-new_branch
jamesqquickApr 25, 2020
bc235d4
#7 ISSUE RESOLVED
JayantDwivediMay 26, 2020
f5dc509
Update Readme.md
lolypartyMay 27, 2020
0fd212e
Using real values for score instead of dummy data
jamesqquickJun 11, 2020
2082d9e
rmeoved color from folder name
jamesqquickJun 11, 2020
70a7115
Merge pull request #10 from lolyparty/patch-1
jamesqquickJun 11, 2020
4b66bcd
Merge branch 'master' of https://github.com/jamesqquick/Build-A-Quiz-…
jamesqquickJun 11, 2020
63d7363
Updated random index of answer to be in all spots
jamesqquickJun 11, 2020
d792edc
Removed console.logs
jamesqquickJun 11, 2020
7730022
Create LICENSE
jamesqquickJan 5, 2021
f3f7a80
Update ReadMe.md
jamesqquickMar 2, 2023
ef07bc7
Update ReadMe.md
jamesqquickMar 2, 2023
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
37 changes: 18 additions & 19 deletions10. Fetch Questions from Local JSON File/end.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
const username = document.getElementById("username");
const saveScoreBtn = document.getElementById("saveScoreBtn");
const finalScore = document.getElementById("finalScore");
const mostRecentScore = localStorage.getItem("mostRecentScore");
const username = document.getElementById('username');
const saveScoreBtn = document.getElementById('saveScoreBtn');
const finalScore = document.getElementById('finalScore');
const mostRecentScore = localStorage.getItem('mostRecentScore');

const highScores = JSON.parse(localStorage.getItem("highScores")) || [];
const highScores = JSON.parse(localStorage.getItem('highScores')) || [];

const MAX_HIGH_SCORES = 5;

finalScore.innerText = mostRecentScore;

username.addEventListener("keyup", () => {
saveScoreBtn.disabled = !username.value;
username.addEventListener('keyup', () => {
saveScoreBtn.disabled = !username.value;
});

saveHighScore = e => {
console.log("clicked the save button!");
e.preventDefault();
saveHighScore = (e) => {
e.preventDefault();

const score = {
score: Math.floor(Math.random() * 100),
name: username.value
};
highScores.push(score);
highScores.sort((a, b) => b.score - a.score);
highScores.splice(5);
const score = {
score: mostRecentScore,
name: username.value,
};
highScores.push(score);
highScores.sort((a, b) => b.score - a.score);
highScores.splice(5);

localStorage.setItem("highScores", JSON.stringify(highScores));
window.location.assign("/");
localStorage.setItem('highScores', JSON.stringify(highScores));
window.location.assign('/');
};
117 changes: 58 additions & 59 deletions10. Fetch Questions from Local JSON File/game.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
const question = document.getElementById("question");
const choices = Array.from(document.getElementsByClassName("choice-text"));
const progressText = document.getElementById("progressText");
const scoreText = document.getElementById("score");
const progressBarFull = document.getElementById("progressBarFull");
const question = document.getElementById('question');
const choices = Array.from(document.getElementsByClassName('choice-text'));
const progressText = document.getElementById('progressText');
const scoreText = document.getElementById('score');
const progressBarFull = document.getElementById('progressBarFull');
let currentQuestion = {};
let acceptingAnswers = false;
let score = 0;
Expand All@@ -11,79 +11,78 @@ let availableQuesions = [];

let questions = [];

fetch("questions.json")
.then(res => {
return res.json();
})
.then(loadedQuestions => {
console.log(loadedQuestions);
questions = loadedQuestions;
startGame();
})
.catch(err => {
console.error(err);
});
fetch('questions.json')
.then((res) => {
return res.json();
})
.then((loadedQuestions) => {
questions = loadedQuestions;
startGame();
})
.catch((err) => {
console.error(err);
});

//CONSTANTS
const CORRECT_BONUS = 10;
const MAX_QUESTIONS = 3;

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

getNewQuestion = () => {
if (availableQuesions.length === 0 || questionCounter >= MAX_QUESTIONS) {
localStorage.setItem("mostRecentScore", score);
//go to the end page
return window.location.assign("/end.html");
}
questionCounter++;
progressText.innerText = `Question ${questionCounter}/${MAX_QUESTIONS}`;
//Update the progress bar
progressBarFull.style.width = `${(questionCounter / MAX_QUESTIONS) * 100}%`;
if (availableQuesions.length === 0 || questionCounter >= MAX_QUESTIONS) {
localStorage.setItem('mostRecentScore', score);
//go to the end page
return window.location.assign('/end.html');
}
questionCounter++;
progressText.innerText = `Question ${questionCounter}/${MAX_QUESTIONS}`;
//Update the progress bar
progressBarFull.style.width = `${(questionCounter / MAX_QUESTIONS) * 100}%`;

const questionIndex = Math.floor(Math.random() * availableQuesions.length);
currentQuestion = availableQuesions[questionIndex];
question.innerText = currentQuestion.question;
const questionIndex = Math.floor(Math.random() * availableQuesions.length);
currentQuestion = availableQuesions[questionIndex];
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];
});

availableQuesions.splice(questionIndex, 1);
acceptingAnswers = true;
availableQuesions.splice(questionIndex, 1);
acceptingAnswers = true;
};

choices.forEach(choice => {
choice.addEventListener("click", e => {
if (!acceptingAnswers) return;
choices.forEach((choice) => {
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'];

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

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

selectedChoice.parentElement.classList.add(classToApply);
selectedChoice.parentElement.classList.add(classToApply);

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

incrementScore = num => {
score += num;
scoreText.innerText = score;
incrementScore =(num) => {
score += num;
scoreText.innerText = score;
};
37 changes: 18 additions & 19 deletions11. Fetch API Questions from Open Trivia API/end.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
const username = document.getElementById("username");
const saveScoreBtn = document.getElementById("saveScoreBtn");
const finalScore = document.getElementById("finalScore");
const mostRecentScore = localStorage.getItem("mostRecentScore");
const username = document.getElementById('username');
const saveScoreBtn = document.getElementById('saveScoreBtn');
const finalScore = document.getElementById('finalScore');
const mostRecentScore = localStorage.getItem('mostRecentScore');

const highScores = JSON.parse(localStorage.getItem("highScores")) || [];
const highScores = JSON.parse(localStorage.getItem('highScores')) || [];

const MAX_HIGH_SCORES = 5;

finalScore.innerText = mostRecentScore;

username.addEventListener("keyup", () => {
saveScoreBtn.disabled = !username.value;
username.addEventListener('keyup', () => {
saveScoreBtn.disabled = !username.value;
});

saveHighScore = e => {
console.log("clicked the save button!");
e.preventDefault();
saveHighScore = (e) => {
e.preventDefault();

const score = {
score: Math.floor(Math.random() * 100),
name: username.value
};
highScores.push(score);
highScores.sort((a, b) => b.score - a.score);
highScores.splice(5);
const score = {
score: mostRecentScore,
name: username.value,
};
highScores.push(score);
highScores.sort((a, b) => b.score - a.score);
highScores.splice(5);

localStorage.setItem("highScores", JSON.stringify(highScores));
window.location.assign("/");
localStorage.setItem('highScores', JSON.stringify(highScores));
window.location.assign('/');
};
Loading

[8]ページ先頭

©2009-2025 Movatter.jp