Skip to content

Instantly share code, notes, and snippets.

@arbianchi
Created June 26, 2017 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arbianchi/b8103100e99107ae9399da202bfe71c9 to your computer and use it in GitHub Desktop.
Save arbianchi/b8103100e99107ae9399da202bfe71c9 to your computer and use it in GitHub Desktop.
Trivia Game Feedback
// ADINA: What you did definitely works, but if you wanted to dynamically generate the input elements with javascript, this is how you would do it:
// var questions = [{
// question: "What was the first full length CGI movie?",
// answers: ["A Bug's Life", "Monsters Inc.", "Toy Story", "The Lion King"],
// correctAnswer: "Toy Story"
// }, {
// question: "Which of these is NOT a name of one of the Spice Girls?",
// answers: ["Sporty Spice", "Fred Spice", "Scary Spice", "Posh Spice"],
// correctAnswer: "Fred Spice"
// }, {
// question: "Which NBA team won the most titles in the 90s?",
// answers: ["New York Knicks", "Portland Trailblazers", "Los Angeles Lakers", "Chicago Bulls"],
// correctAnswer: "Chicago Bulls"
// }]
// for (var i = 0; i < questions.length; i++) {
// panel.append("<h2>" + questions[i].question + "</h2>");
// for (var j = 0; j < questions[i].answers.length; j++) {
// panel.append("<input type='radio' name='question-" + i +
// "' value='" + questions[i].answers[j] + "''>" + questions[i].answers[j]);
// }
// }
$(document).ready(function() {
//setting a variable "right" for questions that are answered correctly
var right = 0;
//setting a variable "wrong" for questions that are answered incorrectly
var wrong = 0;
//settingn a variable "unanswered" for questions that are not answered
var unanswered = 0;
//setting a variable "audio" for the sound that will be played once you click the start button
var audio = new Audio("assets/audio/TarHeelsFightSong.mp3");
//setting a variable "number" that is equal to 120 seconds. This will be the time left to guess answers
var number = 120;
//declaring a variable interval for later use
var interval;
//selecting the element with the id "start" and when it is clicked run the function "begin"
$("#start").on("click", begin);
/* setting the function "begin". the previously declared variable "interval" is set equal
to the value of the function setInterval with the argument of the function decrement, and the
interval set to 1000 milliseconds.
the variable audio is given an event listener that waits until the sound has ended and then
restarts it
*/
function begin() {
interval = setInterval(decrement, 1000);
audio.addEventListener('ended', function() {
this.currentTime = 0;
this.play();
}, false);
audio.play();
}
function stop() {
$("#timeLeft").html("<h1></h1>");
clearInterval(interval);
}
/*setting a function decrement that subtracts 1 from the value of the variable number. This
action is displayed in the element with an id "timeLeft" where an h1 object is created
*/
function decrement() {
number--;
$("#timeLeft").html("<h1>" + number + "</h1>");
if (number === 0) {
alert("Time Up!");
stop();
audio.pause();
}
}
//
$("#submit").closest("form").submit(function() {
var score = 0;
var numberOfQuestions = 10;
var answers = ["c", "a", "a", "b", "a", "d", "b", "d", "b", "a"];
var q1 = document.forms["quizForm"]["q1"].value;
var q2 = document.forms["quizForm"]["q2"].value;
var q3 = document.forms["quizForm"]["q3"].value;
var q4 = document.forms["quizForm"]["q4"].value;
var q5 = document.forms["quizForm"]["q5"].value;
var q6 = document.forms["quizForm"]["q6"].value;
var q7 = document.forms["quizForm"]["q7"].value;
var q8 = document.forms["quizForm"]["q8"].value;
var q9 = document.forms["quizForm"]["q9"].value;
var q10 = document.forms["quizForm"]["q10"].value;
for (var i = 1; i <= numberOfQuestions; i++) {
if (eval('q' + i) == "") {
alert("You missed question number " + i);
}
for (var i = 1; i <= numberOfQuestions; i++) {
if (eval('q' + 1) == answers[i - 1]) {
score++;
}
}
var results = $("#results").html("<h2>You Scored " + score + " points out of " + numberOfQuestions + "</h2>")
alert("You Scored " + score + " points out of " + numberOfQuestions);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment