From 274cb91bcd39db16c8dfddc5cae36ef08eb7cf63 Mon Sep 17 00:00:00 2001 From: Nathan Daniels Date: Fri, 5 Nov 2021 11:39:48 -0500 Subject: [PATCH 1/7] boilerplate and dependencies --- .gitignore | 1 + game.js | 0 package-lock.json | 103 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 6 +++ 4 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 game.js create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/game.js b/game.js new file mode 100644 index 0000000..e69de29 diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..ea60c1f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,103 @@ +{ + "name": "build-a-bot", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "dependencies": { + "axios": "^0.24.0", + "prompt-sync": "^4.2.0" + } + }, + "node_modules/ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", + "engines": { + "node": ">=6" + } + }, + "node_modules/axios": { + "version": "0.24.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz", + "integrity": "sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==", + "dependencies": { + "follow-redirects": "^1.14.4" + } + }, + "node_modules/follow-redirects": { + "version": "1.14.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.5.tgz", + "integrity": "sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/prompt-sync": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/prompt-sync/-/prompt-sync-4.2.0.tgz", + "integrity": "sha512-BuEzzc5zptP5LsgV5MZETjDaKSWfchl5U9Luiu8SKp7iZWD5tZalOxvNcZRwv+d2phNFr8xlbxmFNcRKfJOzJw==", + "dependencies": { + "strip-ansi": "^5.0.0" + } + }, + "node_modules/strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "dependencies": { + "ansi-regex": "^4.1.0" + }, + "engines": { + "node": ">=6" + } + } + }, + "dependencies": { + "ansi-regex": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", + "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==" + }, + "axios": { + "version": "0.24.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.24.0.tgz", + "integrity": "sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==", + "requires": { + "follow-redirects": "^1.14.4" + } + }, + "follow-redirects": { + "version": "1.14.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.5.tgz", + "integrity": "sha512-wtphSXy7d4/OR+MvIFbCVBDzZ5520qV8XfPklSN5QtxuMUJZ+b0Wnst1e1lCDocfzuCkHqj8k0FpZqO+UIaKNA==" + }, + "prompt-sync": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/prompt-sync/-/prompt-sync-4.2.0.tgz", + "integrity": "sha512-BuEzzc5zptP5LsgV5MZETjDaKSWfchl5U9Luiu8SKp7iZWD5tZalOxvNcZRwv+d2phNFr8xlbxmFNcRKfJOzJw==", + "requires": { + "strip-ansi": "^5.0.0" + } + }, + "strip-ansi": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", + "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", + "requires": { + "ansi-regex": "^4.1.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..0031064 --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "axios": "^0.24.0", + "prompt-sync": "^4.2.0" + } +} From c93899dacb33386b643db9e5497f0ff6ba3bd4d6 Mon Sep 17 00:00:00 2001 From: Nathan Daniels Date: Fri, 5 Nov 2021 12:15:02 -0500 Subject: [PATCH 2/7] getQuestions.js is complete --- getQuestions.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 getQuestions.js diff --git a/getQuestions.js b/getQuestions.js new file mode 100644 index 0000000..762cf59 --- /dev/null +++ b/getQuestions.js @@ -0,0 +1,33 @@ +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:\nfilm\nmusic\nvideo games\nanime'); + category = prompt('Enter choice here: ').toLowerCase(); + } + while (!difficulties.includes(difficulty)) { + console.log('Pick a difficulty:\neasy\nmedium\nhard'); + difficulty = prompt('Enter choice here: ').toLowerCase(); + } + + let questions = await axios.get(`https://opentdb.com/api.php?amount=5&category=${categories[category]}&difficulty=${difficulty}`) + + + return questions; +} + +module.exports = { + getQuestions +} \ No newline at end of file From 009199201147dd96f47ea998ac34cc0b0a3de4a8 Mon Sep 17 00:00:00 2001 From: Nathan Daniels Date: Fri, 5 Nov 2021 12:31:37 -0500 Subject: [PATCH 3/7] getQuestions now replaces html codes with actual characters --- getQuestions.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/getQuestions.js b/getQuestions.js index 762cf59..f5f5a2c 100644 --- a/getQuestions.js +++ b/getQuestions.js @@ -24,7 +24,10 @@ const getQuestions = async () => { let questions = await axios.get(`https://opentdb.com/api.php?amount=5&category=${categories[category]}&difficulty=${difficulty}`) - + questions = questions.data.results; + questions.forEach(item => { + item.question = item.question.replaceAll('"', '') + .replaceAll(''', "'")}); return questions; } From 292b13df7765a5f0dd16077d1b6871de98486f55 Mon Sep 17 00:00:00 2001 From: Nathan Daniels Date: Fri, 5 Nov 2021 14:13:20 -0500 Subject: [PATCH 4/7] Added shuffler --- game.js | 10 ++++++++++ getQuestions.js | 7 ++++--- shuffler.js | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 shuffler.js diff --git a/game.js b/game.js index e69de29..9826599 100644 --- a/game.js +++ b/game.js @@ -0,0 +1,10 @@ +const prompt = require('prompt-sync'); +const { shuffler } = require('.//shuffler'); +const { getQuestions } = require('./getQuestions'); + +const playGame = async () => { + let questions = await getQuestions(); + +} + +playGame(); \ No newline at end of file diff --git a/getQuestions.js b/getQuestions.js index f5f5a2c..eb081ef 100644 --- a/getQuestions.js +++ b/getQuestions.js @@ -23,13 +23,14 @@ const getQuestions = async () => { } let questions = await axios.get(`https://opentdb.com/api.php?amount=5&category=${categories[category]}&difficulty=${difficulty}`) - questions = questions.data.results; questions.forEach(item => { item.question = item.question.replaceAll('"', '') - .replaceAll(''', "'")}); + .replaceAll(''', "'") + }) + return questions; -} +} module.exports = { getQuestions diff --git a/shuffler.js b/shuffler.js new file mode 100644 index 0000000..222e5ab --- /dev/null +++ b/shuffler.js @@ -0,0 +1,17 @@ +const shuffler = (arr) => { + const indexArr = []; + const length = arr.length; + while (indexArr.length < length) { + const randomIndex = Math.floor(Math.random() * length); + + if (!indexArr.includes(randomIndex)) { + indexArr.push(randomIndex); + } + } + + return indexArr; + } + + module.exports = { + shuffler + } \ No newline at end of file From 8f1337050111ead316e7a3d13d2b228e5c206ae7 Mon Sep 17 00:00:00 2001 From: Gustavo Mendoza Date: Fri, 5 Nov 2021 14:42:24 -0500 Subject: [PATCH 5/7] gus_shuffle --- .DS_Store | Bin 0 -> 6148 bytes game.js | 11 +++++++++++ untitled folder/.DS_Store | Bin 0 -> 6148 bytes untitled folder/gus_shuffle.js | 16 ++++++++++++++++ 4 files changed, 27 insertions(+) create mode 100644 .DS_Store create mode 100644 untitled folder/.DS_Store create mode 100644 untitled folder/gus_shuffle.js diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..699b1095cbc3d3e4378aeb5f0afdacc92cfd05d5 GIT binary patch literal 6148 zcmeHKyG{Z@6g{ID@sWU?jmiFiBsA75Sqcja21thUBhTNOnd+yH6naA1e%nkrk8?~!I89w@?LW&-< zS?uQ=dk1}v)xr&<^s|qIYB;RM60p;3M`cZrVurv8kgkH)~UtGSsT#L=wi|?b7(0H i{y4UY9L2kIYpe?-A%-3^hb*D#kARgygDUW+3VZ_M8I$4w literal 0 HcmV?d00001 diff --git a/game.js b/game.js index e69de29..fafff5a 100644 --- a/game.js +++ b/game.js @@ -0,0 +1,11 @@ +// const axios = require("axios"); +// const prompt = require('prompt-sync') + + +// axios.get(`https://opentdb.com/api.php?amount=5`).then(function(response){ + +// console.log(`response.data`); +// } +// ); + + diff --git a/untitled folder/.DS_Store b/untitled folder/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5d72981867985ea65afcaa5e7c05968dfcd4e31b GIT binary patch literal 6148 zcmeHKyH3PF3>?E1Cn23uQtlTZ@dvBZQBd;%pyMeN>5Az3bbR(PwiiUmK}mtekv&<* zv#U%I#{g`-f4BrX086?f-aO3BpS#cOq-Bgq=NT m4AF{-(Tch7R($@YuJ}El*M?(a(3uZ9Q9lB%i%bgqg#zz$awahV literal 0 HcmV?d00001 diff --git a/untitled folder/gus_shuffle.js b/untitled folder/gus_shuffle.js new file mode 100644 index 0000000..dc7dc3d --- /dev/null +++ b/untitled folder/gus_shuffle.js @@ -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; \ No newline at end of file From 9f5d1d21d21510d18c4d2a790233b8c672ee1b4e Mon Sep 17 00:00:00 2001 From: Nathan Daniels Date: Fri, 5 Nov 2021 15:36:33 -0500 Subject: [PATCH 6/7] Game is working --- game.js | 38 ++++++++++++++++++++++++++++++++++++-- getQuestions.js | 6 +++--- question.js | 18 ++++++++++++++++++ shuffler.js | 8 ++++---- 4 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 question.js diff --git a/game.js b/game.js index 9826599..8c95b49 100644 --- a/game.js +++ b/game.js @@ -1,9 +1,43 @@ -const prompt = require('prompt-sync'); -const { shuffler } = require('.//shuffler'); +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`); + + const userAnswer = prompt('Choose the correct letter: ').toLowerCase(); + 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++; + } } diff --git a/getQuestions.js b/getQuestions.js index eb081ef..d9513e1 100644 --- a/getQuestions.js +++ b/getQuestions.js @@ -14,15 +14,15 @@ const getQuestions = async () => { let category; let difficulty; while (!categories[category]) { - console.log('Pick a category:\nfilm\nmusic\nvideo games\nanime'); + 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:\neasy\nmedium\nhard'); + 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}`) + 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('"', '') diff --git a/question.js b/question.js new file mode 100644 index 0000000..f28278f --- /dev/null +++ b/question.js @@ -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 +} \ No newline at end of file diff --git a/shuffler.js b/shuffler.js index 222e5ab..37d374a 100644 --- a/shuffler.js +++ b/shuffler.js @@ -1,8 +1,8 @@ -const shuffler = (arr) => { +const shuffler = () => { const indexArr = []; - const length = arr.length; - while (indexArr.length < length) { - const randomIndex = Math.floor(Math.random() * length); + + while (indexArr.length < 4) { + const randomIndex = Math.floor(Math.random() * 4); if (!indexArr.includes(randomIndex)) { indexArr.push(randomIndex); From 696114854e883ec0a596c7f8eeffdc3f673f35fd Mon Sep 17 00:00:00 2001 From: Nathan Daniels Date: Fri, 5 Nov 2021 15:47:56 -0500 Subject: [PATCH 7/7] Added extra functionality to user answer --- game.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/game.js b/game.js index c5d64da..5bc235a 100644 --- a/game.js +++ b/game.js @@ -18,7 +18,10 @@ const playGame = async () => { console.log(`c. ${answers.c}`); console.log(`d. ${answers.d}\n`); - const userAnswer = prompt('Choose the correct letter: ').toLowerCase(); + 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');