Advertisement
InasAwad

Untitled

Feb 5th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. var Question = function ( question, answers, right){
  2. this.question = question;
  3. this.answers = answers;
  4. this.right = right;
  5. }
  6.  
  7. var q1 = new Question ('* What is the best programming language?' ,
  8. ['Java', 'JavaScript', 'Python'],
  9. 1);
  10.  
  11. var q2 = new Question('* What is the best way to manage it',
  12. ['Study', 'Practice', 'Both'],
  13. 2);
  14.  
  15. var q3 = new Question('* Do you think you will be a programmer one day?',
  16. ['YESSSS', 'Maybe', 'Unfortunately no'],
  17. 0);
  18.  
  19. var q4 = new Question('* How long will it take to be a programmer?',
  20. ['Don\'t know', '6 months', 'Yeaaars'],
  21. 1);
  22.  
  23. Question.prototype.displayQuestion = function(){
  24. console.log(this.question);
  25.  
  26. for(var i = 0; i<this.answers.length; i++){
  27. console.log(i + '. ' + this.answers[i]);
  28. }
  29.  
  30. }
  31.  
  32. Question.prototype.checkIfCorrect = function(ans, comeBack){
  33. var po;
  34. if (ans === this.right){
  35. console.log('Correct answer! Good Job!');
  36. po = comeBack(true);
  37. } else {
  38. console.log('Sorry not corret. Try again :)');
  39. po = comeBack(false);
  40. }
  41. // this.displayPoints(po);
  42. }
  43.  
  44. //Question.prototype.displayPoints = function(points){
  45. // console.log('Your score is ' + points +'!');
  46. // console.log('*^*^*^*^**^*^*');
  47. //}
  48.  
  49. function points(){
  50. var po = 0;
  51. return function(right){
  52. if(right){
  53. po++;
  54. console.log('Your score is 1 point more! Now it\'s ' + po); // I added this part.
  55. console.log('*^*^*^*^*^*^*'); // also this.
  56. } else {
  57. console.log('Your score is still ' + po + '.'); // and this.
  58. console.log('*^*^*^*^*^*^*'); // and this :)
  59. }
  60. return po;
  61. }
  62. }
  63. var keepPoints = points();
  64.  
  65. var questions = [q1, q2, q3, q4];
  66.  
  67.  
  68. function nextQuestion(){
  69.  
  70. var n = Math.floor(Math.random() * questions.length);
  71. questions[n].displayQuestion();
  72.  
  73. var answer = prompt('Please enter the your answer: ');
  74. if (answer !== 'exit'){
  75. questions[n].checkIfCorrect(parseInt(answer
  76. ), keepPoints);
  77. nextQuestion();
  78. }
  79. }
  80. nextQuestion();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement