Skip to content
Open
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
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
47 changes: 47 additions & 0 deletions game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const prompt = require('prompt-sync')();
const { getQuestions } = require('./getQuestions');
const { question } = require('./question');

const playGame = async () => {
prompt('Click enter to play');
let questions = await getQuestions();
let index = 1;
let score = 0;

while (questions.length) {
const currentQuestion = questions.shift();
const answers = await question(currentQuestion);
console.log(`Question ${index}\n`);
console.log(currentQuestion.question + '\n');
console.log(`a. ${answers.a}`);
console.log(`b. ${answers.b}`);
console.log(`c. ${answers.c}`);
console.log(`d. ${answers.d}\n`);

let userAnswer = prompt('Choose the correct letter: ').toLowerCase();
while(!answers[userAnswer]) {
userAnswer = prompt('Answer must be a, b, c or d. Guess again: ');
}
if (answers[userAnswer] === currentQuestion.correct_answer) {
score++;
console.log('You are correct');
} else {
console.log(`That is incorrect. The correct answer is ${currentQuestion.correct_answer}`);
}
console.log(`Current Score: ${score}/${index}\n`);
if (index < 5) {
prompt('Click enter to go to the next question\n');
} else {
const gameEnd = prompt('Would you like to play again? Y or N?\n').toLowerCase();
if (gameEnd === 'y') {
playGame();
} else {
console.log('Thanks for playing!');
}
}
index++;
}

}

playGame();
37 changes: 37 additions & 0 deletions getQuestions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const axios = require('axios');
const prompt = require('prompt-sync')();

const getQuestions = async () => {
const categories = {
'film': 11,
'music': 12,
'video games': 15,
'anime': 31
};

const difficulties = ['easy', 'medium', 'hard'];

let category;
let difficulty;
while (!categories[category]) {
console.log('Pick a category:\n\nfilm\nmusic\nvideo games\nanime\n');
category = prompt('Enter choice here: ').toLowerCase();
}
while (!difficulties.includes(difficulty)) {
console.log('Pick a difficulty:\n\neasy\nmedium\nhard\n');
difficulty = prompt('Enter choice here: ').toLowerCase();
}

let questions = await axios.get(`https://opentdb.com/api.php?amount=5&category=${categories[category]}&difficulty=${difficulty}&type=multiple`)
questions = questions.data.results;
questions.forEach(item => {
item.question = item.question.replaceAll('&quot;', '')
.replaceAll('&#039;', "'")
})

return questions;
}

module.exports = {
getQuestions
}
103 changes: 103 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"axios": "^0.24.0",
"prompt-sync": "^4.2.0"
}
}
18 changes: 18 additions & 0 deletions question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { shuffler } = require('./shuffler');

const question = async (currentQuestion) => {
const answers = currentQuestion.incorrect_answers
.concat(currentQuestion.correct_answer)
const indexArr = shuffler();
const answerObj = {
'a': answers[indexArr[0]],
'b': answers[indexArr[1]],
'c': answers[indexArr[2]],
'd': answers[indexArr[3]]
};
return answerObj;
}

module.exports = {
question
}
17 changes: 17 additions & 0 deletions shuffler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const shuffler = () => {
const indexArr = [];

while (indexArr.length < 4) {
const randomIndex = Math.floor(Math.random() * 4);

if (!indexArr.includes(randomIndex)) {
indexArr.push(randomIndex);
}
}

return indexArr;
}

module.exports = {
shuffler
}
Binary file added untitled folder/.DS_Store
Binary file not shown.
16 changes: 16 additions & 0 deletions untitled folder/gus_shuffle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function shuffleArry (array) {

// While there remain elements to shuffle...



while (currentIndex != 0) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);



}

return array;