Skip to content

Instantly share code, notes, and snippets.

@arbianchi
Created June 24, 2017 17:29
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/3577a21c003d44fa8f8da949fe9bd355 to your computer and use it in GitHub Desktop.
Save arbianchi/3577a21c003d44fa8f8da949fe9bd355 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() {
console.log()
var sTime = new Date().getTime();
var countDown = 30;
console.log("Test One");
$("#quiz").hide();
$("#validateform").click(validateform);
document.getElementById("startButton").onclick = function() {myFunction()};
function myFunction() {
document.getElementById("startButton").innerHTML = "Good Luck!"
//$("#quiz").prepend();
$("#bodytext").empty(); // clear body text
$("#headline").empty(); // clear headline
$("#quiz").show();
///// above works with onclick function it empties some text lines
UpdateTime();
var counter = setInterval(UpdateTime, 500);
//// timer above works, countdown from 90 while user is taking quiz
// var amountChecked = 0;
// for (var k = 1; k < 9; k++) {
// var radiosCheck = document.getElementsByName('group' + k);
// for ( var l = 0; l < radiosCheck.length; l++) {
// var radiosCheck = radiosCheck[l];
// if(radiosCheck < 8) {
// alert("You are missing a question, please review");
// } // closes alert statment
// } // closes for var radiosCheck
// } // closes for k loop
}
function UpdateTime() {
var cTime = new Date().getTime();
var diff = cTime - sTime;
var seconds = countDown - Math.floor(diff / 1000);
document.getElementById("headline").innerHTML = "You have " + seconds + " seconds left!"; //show seconds
if (seconds === 0 || seconds <0) {
document.getElementById("headline").innerHTML = "Your Time Is Up!!";
} // close if statement
} // close function updatetime
function getRBtnName(GrpName) {
//ADINA: Use more explicit names for your variables, even if it takes up more space. It makes it easier for you and others to go back and read your code.
var sel = document.getElementsByName(GrpName);
var fnd = -1;
var str = '';
for (var i=0; i<sel.length; i++) {
if (sel[i].checked == true) { str = sel[i].value; fnd = i; }
}
return str;
} // closes function getRBtnName
function validateform() {
// ADINA: Any time you notice yourself repeating code but only changing 1 or 2 things, consider using a for loop.
var q1 = getRBtnName('q1');
var q2 = getRBtnName('q2');
var q3 = getRBtnName('q3');
var q4 = getRBtnName('q4');
var q5 = getRBtnName('q5');
var q6 = getRBtnName('q6');
var q7 = getRBtnName('q7');
var q8 = getRBtnName('q8');
var qans = q1+q2+q3+q4+q5+q6+q7+q8;
if (qans.length < 8) { alert('Missing selection'); return; }
else finalClick();
} // closes function validate form
function finalClick() {
alert("Hi!");
var amountCorrect = 0;
for(var i = 0; i <= 8; i++) {
if(radio.value == "correct" && radio.checked) {
amountCorrect++;
}
}
alert("Correct Responses: " + amountCorrect);
}// closes for i loop
// closes fu
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment