Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. Question.prototype.displayRandomQuestion = function() // function to loop through all the questions and answers
  2. {
  3. console.log(this.question);
  4.  
  5. for(var i = 0; i < this.answer.length; i++)
  6. {
  7. console.log(i + " : " + this.answer[i]);
  8. }
  9. }
  10.  
  11. Question.prototype.checkAnswer = function(ans)
  12. {
  13. if(ans === this.correct)
  14. {
  15. console.log('correct output!!');
  16. }
  17. else
  18. {
  19. console.log('Wrong answer !!');
  20. }
  21. }
  22.  
  23.  
  24. // questions to be logged on the console arg(1), possible outcomes arg(2), correct ans arg(3)
  25.  
  26. var ques1 = new Question('Is Mclaren Speedtail better than a Bugatti Chiron ??', ['yes', 'no'], 0);
  27.  
  28. var ques2 = new Question('Which is the best Hypercar Brand ?', ['Porsche', 'Ferrari', 'Mclaren'], 2);
  29.  
  30. var ques3 = new Question('What are the best Engine Types ?', ['Naturally Aspirated', 'Hybrid', 'Turbo-charged', 'Supercharged'], 1);
  31.  
  32. var questions = [ques1, ques2, ques3];
  33.  
  34. var getRandom = Math.floor(Math.random() * questions.length); // generating random question on the console
  35.  
  36. questions[getRandom].displayRandomQuestion();
  37.  
  38. var answer = parseInt (prompt('Please select the correct answer !!'));
  39.  
  40. questions[getRandom].checkAnswer(answer);
  41. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement