Skip to content

Instantly share code, notes, and snippets.

@tjstalcup
Created June 11, 2015 19:10
Show Gist options
  • Save tjstalcup/878e323cd2cc44174d3e to your computer and use it in GitHub Desktop.
Save tjstalcup/878e323cd2cc44174d3e to your computer and use it in GitHub Desktop.
Example of moving the showQuestion code into the Object itself
// store questions and answers
var allQuestions = {
"what year was the first star wars movie released?",
"what type of species is Chewbacca?",
"on what planet does jabba the hutt live on?",
"what is the name of boba fett's ship",
"who is luke skywalker's father?"
};
var allChoices = {
["1972", "1977", "1980", "1983"],
["ewok", "gungan", "neti", "wookiee"],
["alderaan", "hoth", "tatooine", "yavin"],
["slave 1", "slave 2", "tantive iv", "death star"],
["obi-wan kenobi", "yoda", "darth vader", "han solo"]
};
function quizQuestion(question, choices, answer) {
this.question = question;
this.choices = choices;
this.answer = answer;
this.showQuestion = function(){
var html = "<p id='question'>" + this.question + "</p><div class='answers'>";
for (var i = 0; i < this.choices.length; i++) {
html += "<input type='radio' class='radio' name='choice' id ='"+i+"' value='"+i+"'><label for='"+i+"'>" + this.choices[i] + "</label>";
};
html += "</div>";
return html;
}
}
var question1 = new quizQuestion {question[0], choices[0], 1};
var question2 = new quizQuestion {question[1], choices[1], 3};
var question3 = new quizQuestion {question[2], choices[2], 2};
var question4 = new quizQuestion {question[3], choices[3], 0};
var question5 = new quizQuestion {question[4], choices[4], 2};
var questionArr = {question1, question2, question3, question4, question5};
function showQuestion() {
var question = questionArr[qCount];
var newQuestion = question.showQuestion();
$('#questions-box').html(newQuestion);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment