Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const readlineSync = require('readline-sync');
- const categories = {
- 'ranking' : 0,
- 'year' : 1,
- 'album': 2,
- 'artist': 3,
- 'genre': 4
- };
- const possibleCats = [['year', 'album'],['album', 'year'],['album', 'artist'],['album','genre'],['artist','album']];
- const gameState = {
- user:'', score:0, questionsAnswered: 0, questions: 5
- };
- function pick4FromCat(category){
- const choices = [];
- const artists = new Set();
- let currentItem = '';
- while (choices.length < 4){
- currentItem = data[Math.floor(Math.random()*data.length)];
- if (!artists.has(currentItem[categories[category]])) {
- artists.add(currentItem[categories[category]]);
- choices.push(currentItem);
- }
- }
- return choices;
- }
- function askQuestionAbout(cat1, cat2){
- const catIndex1 = categories[cat1];
- const catIndex2 = categories[cat2];
- const choices = pick4FromCat(cat1);
- const answerIndex = Math.floor(Math.random()*4);
- let correctAnswer;
- correctAnswer = choices[answerIndex];
- let question;
- if(cat1 === 'year' && cat2 === 'album'){
- question = `Which of the following years did ${correctAnswer[categories['artist']]} release the album, ${correctAnswer[catIndex2]}?`
- }
- if(cat1 === 'album'){
- if (cat2 === 'year') question =`Which of the following albums was released in ${correctAnswer[catIndex2]}?`
- if (cat2 === 'artist') question = `${correctAnswer[catIndex2]} created which of the following albums?`
- if (cat2 === 'genre') question = `Which of the following is a ${correctAnswer[catIndex2]} album?`
- }
- if(cat1 === 'artist'){
- if(cat2 === 'album') question =`The album ${correctAnswer[catIndex2]} was released by which of the following artists?`
- }
- let userAnswer = Number(readlineSync.question(
- `${question}
- 1: ${choices[0][catIndex1]}
- 2: ${choices[1][catIndex1]}
- 3: ${choices[2][catIndex1]}
- 4: ${choices[3][catIndex1]}\n `));
- while(isNaN(userAnswer) || userAnswer > 4 || userAnswer < 1){
- userAnswer = Number(readlineSync.question('\nplease enter a number between 1 and 4! \n'))
- }
- console.log(
- `\nYour answer was \n ${userAnswer}: ${choices[userAnswer-1][catIndex1]}\n\nthe correct answer is \n ${answerIndex+1}: ${correctAnswer[catIndex1]}\n`);
- updateState((userAnswer-1) === answerIndex);
- }
- function updateState(correct){
- gameState.questionsAnswered++
- if(correct){
- gameState.score++;
- console.log('Congrats! You were correct');
- }else{
- console.log('Ya missed this one. Best of luck on the next!')
- }
- console.log(`Current score: ${gameState.score}/${gameState.questionsAnswered}`);
- console.log(`${gameState.questions - gameState.questionsAnswered} questions remaining!`)
- }
- function playGame(){
- gameState.user = readlineSync.question('What is your name? \n')
- console.log(`Welcome to the quiz, ${gameState.user}!`)
- while(gameState.questionsAnswered < gameState.questions){
- askQuestionAbout(...possibleCats[gameState.questionsAnswered%possibleCats.length])
- }
- console.log('---------')
- console.log(`Congrats on making it through the quiz, ${gameState.user}! Was it hard?`)
- console.log(`final score:${gameState.score}/${gameState.questionsAnswered}` )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement