Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const readlineSync = require('readline-sync');
  2.  
  3. const categories = {
  4.   'ranking' : 0,
  5.   'year' : 1,
  6.   'album': 2,
  7.   'artist': 3,
  8.   'genre': 4
  9. };
  10. const possibleCats = [['year', 'album'],['album', 'year'],['album', 'artist'],['album','genre'],['artist','album']];
  11.  
  12. const gameState = {
  13.   user:'', score:0, questionsAnswered: 0, questions: 5
  14.   };
  15.  
  16. function pick4FromCat(category){
  17.   const choices = [];
  18.   const artists = new Set();
  19.   let currentItem = '';
  20.   while (choices.length < 4){
  21.     currentItem = data[Math.floor(Math.random()*data.length)];
  22.     if (!artists.has(currentItem[categories[category]])) {
  23.       artists.add(currentItem[categories[category]]);
  24.       choices.push(currentItem);
  25.     }
  26.   }
  27.   return choices;
  28. }
  29.  
  30.  
  31. function askQuestionAbout(cat1, cat2){
  32.   const catIndex1 = categories[cat1];
  33.   const catIndex2 = categories[cat2];
  34.   const choices = pick4FromCat(cat1);
  35.   const answerIndex = Math.floor(Math.random()*4);
  36.   let correctAnswer;
  37.   correctAnswer = choices[answerIndex];
  38.   let question;
  39.  
  40.   if(cat1 === 'year' && cat2 === 'album'){
  41.     question = `Which of the following years did ${correctAnswer[categories['artist']]} release the album, ${correctAnswer[catIndex2]}?`
  42.   }
  43.   if(cat1 === 'album'){
  44.     if (cat2 === 'year') question =`Which of the following albums was released in ${correctAnswer[catIndex2]}?`
  45.     if (cat2 === 'artist') question = `${correctAnswer[catIndex2]} created which of the following albums?`
  46.     if (cat2 === 'genre') question = `Which of the following is a ${correctAnswer[catIndex2]} album?`
  47.   }
  48.   if(cat1 === 'artist'){
  49.     if(cat2 === 'album') question =`The album ${correctAnswer[catIndex2]} was released by which of the following artists?`
  50.   }
  51.   let userAnswer = Number(readlineSync.question(
  52.     `${question}
  53.   1: ${choices[0][catIndex1]}
  54.   2: ${choices[1][catIndex1]}
  55.   3: ${choices[2][catIndex1]}
  56.   4: ${choices[3][catIndex1]}\n `));
  57.  
  58.   while(isNaN(userAnswer) || userAnswer > 4 || userAnswer < 1){
  59.     userAnswer = Number(readlineSync.question('\nplease enter a number between 1 and 4! \n'))
  60.     }
  61.  
  62.   console.log(
  63.     `\nYour answer was \n  ${userAnswer}: ${choices[userAnswer-1][catIndex1]}\n\nthe correct answer is \n  ${answerIndex+1}: ${correctAnswer[catIndex1]}\n`);
  64.     updateState((userAnswer-1) === answerIndex);
  65. }
  66.  
  67. function updateState(correct){
  68.   gameState.questionsAnswered++
  69.   if(correct){
  70.     gameState.score++;
  71.     console.log('Congrats! You were correct');
  72.   }else{
  73.     console.log('Ya missed this one. Best of luck on the next!')
  74.   }
  75.     console.log(`Current score: ${gameState.score}/${gameState.questionsAnswered}`);
  76.     console.log(`${gameState.questions - gameState.questionsAnswered} questions remaining!`)
  77. }
  78.  
  79. function playGame(){
  80.   gameState.user = readlineSync.question('What is your name? \n')
  81.   console.log(`Welcome to the quiz, ${gameState.user}!`)
  82.  
  83.   while(gameState.questionsAnswered < gameState.questions){
  84.     askQuestionAbout(...possibleCats[gameState.questionsAnswered%possibleCats.length])
  85.   }
  86.   console.log('---------')
  87.   console.log(`Congrats on making it through the quiz, ${gameState.user}! Was it hard?`)
  88.   console.log(`final score:${gameState.score}/${gameState.questionsAnswered}` )
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement