daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Jun 19th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var panel = $('#quiz-area');
  2. var countStartNumber = 30;
  3.  
  4.  
  5. ///////////////////////////////////////////////////////////////////////////////
  6.  
  7. //CLICK EVENTS
  8.  
  9. ///////////////////////////////////////////////////////////////////////////////
  10.  
  11. $(document).on('click', '#start-over', function(e) {
  12.   game.reset();
  13. });
  14.  
  15. $(document).on('click', '.answer-button', function(e) {
  16.   game.clicked(e);
  17. });
  18.  
  19. $(document).on('click', '#start', function(e) {
  20.   $('#subwrapper').prepend('<h2>Time Remaining: <span id="counter-number">30</span> Seconds</h2>');
  21.   game.loadQuestion();
  22. });
  23.  
  24. ///////////////////////////////////////////////////////////////////////////////
  25.  
  26.  
  27. //Question set
  28.  
  29.  
  30. ///////////////////////////////////////////////////////////////////////////////
  31.  
  32. var questions = [{
  33.   question: "What was the first full length CGI movie?",
  34.   answers: ["A Bug's Life", "Monsters Inc.", "Toy Story", "The Lion King"],
  35.   correctAnswer: "Toy Story",
  36.   image:"assets/images/toystory.gif"
  37. }, {
  38.   question: "Which of these is NOT a name of one of the Spice Girls?",
  39.   answers: ["Sporty Spice", "Fred Spice", "Scary Spice", "Posh Spice"],
  40.   correctAnswer: "Fred Spice",
  41.   image:"assets/images/spicegirls.gif"
  42. }, {
  43.   question: "Which NBA team won the most titles in the 90s?",
  44.   answers: ["New York Knicks", "Portland Trailblazers", "Los Angeles Lakers", "Chicago Bulls"],
  45.   correctAnswer: "Chicago Bulls",
  46.   image:"assets/images/bulls.gif"
  47. }, {
  48.   question: 'Which group released the hit song, "Smells Like Teen Spirit"?',
  49.   answers: ["Nirvana", "Backstreet Boys", "The Offspring", "No Doubt"],
  50.   correctAnswer: "Nirvana",
  51.   image:"assets/images/nirvanabark.gif"
  52. }, {
  53.   question: 'Which popular Disney movie featured the song, "Circle of Life"?',
  54.   answers: ["Aladdin", "Hercules", "Mulan", "The Lion King"],
  55.   correctAnswer: "The Lion King",
  56.   image:"assets/images/lionking.gif"
  57. }, {
  58.   question: 'Finish this line from the Fresh Prince of Bel-Air theme song: "I whistled for a cab and when it came near, the license plate said..."',
  59.   answers: ["Dice", "Mirror", "Fresh", "Cab"],
  60.   correctAnswer: "Fresh",
  61.   image:"assets/images/fresh.gif"
  62. }, {
  63.   question: "What was Doug's best friend's name?",
  64.   answers: ["Skeeter", "Mark", "Zach", "Cody"],
  65.   correctAnswer: "Skeeter",
  66.   image:"assets/images/skeeter.gif"
  67. }, {
  68.   question: "What was the name of the principal at Bayside High in Saved By The Bell?",
  69.   answers: ["Mr.Zhou", "Mr.Driggers", "Mr.Belding", "Mr.Page"],
  70.   correctAnswer: "Mr.Belding",
  71.   image:"assets/images/belding.gif"
  72. }];
  73.  
  74.  
  75.  
  76.  
  77. var game = {
  78.   questions:questions,
  79.   currentQuestion:0,
  80.   counter:countStartNumber,
  81.   correct:0,
  82.   incorrect:0,
  83.   countdown: function(){
  84.     game.counter--;
  85.     $('#counter-number').html(game.counter);
  86.  
  87.     if (game.counter === 0){
  88.       console.log('TIME UP');
  89.       game.timeUp();
  90.     }
  91.   },
  92.   loadQuestion: function(){
  93.     timer = setInterval(game.countdown, 1000);
  94.     panel.html('<h2>' + questions[this.currentQuestion].question + '</h2>' );
  95.     for (var i = 0; i<questions[this.currentQuestion].answers.length; i++){
  96.       panel.append('<button class="answer-button" id="button"' + 'data-name="' + questions[this.currentQuestion].answers[i] + '">' + questions[this.currentQuestion].answers[i]+ '</button>');
  97.     }
  98.   },
  99.   nextQuestion: function(){
  100.     game.counter = countStartNumber;
  101.     $('#counter-number').html(game.counter);
  102.     game.currentQuestion++;
  103.     game.loadQuestion();
  104.   },
  105.   timeUp: function (){
  106.     clearInterval(timer);
  107.     $('#counter-number').html(game.counter);
  108.  
  109.     panel.html('<h2>Out of Time!</h2>');
  110.     panel.append('<h3>The Correct Answer was: ' + questions[this.currentQuestion].correctAnswer);
  111.     panel.append('<img src="' + questions[this.currentQuestion].image + '" />');
  112.  
  113.     if (game.currentQuestion === questions.length - 1){
  114.       setTimeout(game.results, 3 * 1000);
  115.     } else {
  116.       setTimeout(game.nextQuestion, 3 * 1000);
  117.     }
  118.   },
  119.   results: function() {
  120.     clearInterval(timer);
  121.  
  122.     panel.html('<h2>All done, heres how you did!</h2>');
  123.     $('#counter-number').html(game.counter);
  124.     panel.append('<h3>Correct Answers: ' + game.correct + '</h3>');
  125.     panel.append('<h3>Incorrect Answers: ' + game.incorrect + '</h3>');
  126.     panel.append('<h3>Unanswered: ' + (questions.length - (game.incorrect + game.correct)) + '</h3>');
  127.     panel.append('<br><button id="start-over">Start Over?</button>');
  128.   },
  129.   clicked: function(e) {
  130.     clearInterval(timer);
  131.  
  132.     if ($(e.target).data("name") === questions[this.currentQuestion].correctAnswer){
  133.       this.answeredCorrectly();
  134.     } else {
  135.       this.answeredIncorrectly();
  136.     }
  137.   },
  138.   answeredIncorrectly: function() {
  139.     game.incorrect++;
  140.     clearInterval(timer);
  141.     panel.html('<h2>Nope!</h2>');
  142.     panel.append('<h3>The Correct Answer was: ' + questions[game.currentQuestion].correctAnswer + '</h3>');
  143.     panel.append('<img src="' + questions[game.currentQuestion].image + '" />');
  144.  
  145.     if (game.currentQuestion === questions.length - 1){
  146.       setTimeout(game.results, 3 * 1000);
  147.     } else {
  148.       setTimeout(game.nextQuestion, 3 * 1000);
  149.     }
  150.   },
  151.   answeredCorrectly: function(){
  152.     clearInterval(timer);
  153.     game.correct++;
  154.     panel.html('<h2>Correct!</h2>');
  155.     panel.append('<img src="' + questions[game.currentQuestion].image + '" />');
  156.  
  157.     if (game.currentQuestion === questions.length - 1){
  158.       setTimeout(game.results, 3 * 1000);
  159.     } else {
  160.       setTimeout(game.nextQuestion, 3 * 1000);
  161.     }
  162.   },
  163.   reset: function(){
  164.     this.currentQuestion = 0;
  165.     this.counter = countStartNumber;
  166.     this.correct = 0;
  167.     this.incorrect = 0;
  168.     this.loadQuestion();
  169.   }
  170. };
RAW Paste Data
Top