Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. var Question = function (question, answers, correctAnswer){
  2. this.question = question;
  3. this.answers = answers;
  4. this.correctAnswer = correctAnswer;
  5. };
  6.  
  7. // Posible Quiz questions
  8.  
  9. var q1 = new Question ("Is JS the coolest programing language?",["1: Yes", "2: No"], 1);
  10. var q2 = new Question ("Who was your online teacher?", ["1: John", "2: Mark", "3: Jonas", "4: Jane"], 3)
  11. var q3 = new Question ("Is coding awesome?", ["1: Fuck Yeah", "2: It's ok", "3: Not realy"], 1)
  12.  
  13. var questions = [q1, q2, q3];
  14.  
  15. function newQ () {
  16. var score = 0;
  17. while (input !== 0){
  18.  
  19. // Select the question
  20.  
  21. var n = Math.floor(Math.random()*questions.length);
  22.  
  23. // Print the question
  24.  
  25. console.log(questions[n].question + "\n" + questions[n].answers.join('\n'));
  26.  
  27. // Get and validate the answer
  28.  
  29. var input = parseInt(prompt("Please select correct answer (just type the number).\nEnter \"0\" to quit the game"));
  30. if (input === questions[n].correctAnswer){
  31. console.log("That's correct!")
  32. score = score + 1;
  33. console.log("-------------------\n Your score is: " + score + "\n-------------------");
  34. }
  35. else if (input === 0) {
  36. console.log("Quiz completed!")
  37. console.log("-------------------\n Your score is: " + score + "\n-------------------");
  38. }
  39. else {
  40. console.log("Wrong answer.");
  41. console.log("-------------------\n Your score is: " + score + "\n-------------------");
  42. };
  43. };
  44. return score;
  45. };
  46.  
  47. newQ();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement