diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..699b109 Binary files /dev/null and b/.DS_Store differ 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..5bc235a --- /dev/null +++ b/game.js @@ -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(); diff --git a/getQuestions.js b/getQuestions.js new file mode 100644 index 0000000..d9513e1 --- /dev/null +++ b/getQuestions.js @@ -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('"', '') + .replaceAll(''', "'") + }) + + return questions; +} + +module.exports = { + getQuestions +} \ No newline at end of file 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" + } +} 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 new file mode 100644 index 0000000..37d374a --- /dev/null +++ b/shuffler.js @@ -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 + } \ No newline at end of file diff --git a/untitled folder/.DS_Store b/untitled folder/.DS_Store new file mode 100644 index 0000000..5d72981 Binary files /dev/null and b/untitled folder/.DS_Store differ 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