Skip to content

Instantly share code, notes, and snippets.

@plusjade
Last active January 26, 2018 15:56
Show Gist options
  • Save plusjade/6160994 to your computer and use it in GitHub Desktop.
Save plusjade/6160994 to your computer and use it in GitHub Desktop.
// array style, just list the damned questions!
var questions = [
{
question: "What color is Pikachu?",
answers: ["Light Blue", "Brown", "Yellow", "Green"],
solution: "Yellow"
}
,
{
question: "What color is Squirtle?",
answers: ["Blue", "Green", "Red", "Black"],
solution: "Blue"
}
]
// iterate over arrays using this:
for(var i = 0; i < questions.length; i++) {
var q = questions[i];
console.log(q.question) // the question
console.log(q.answers) // the answers
console.log(q.solution) // the solution
}
// dictionary style so you can lookup a question by key
var questionDictionary = {
pikachu : {
question: "What color is Pikachu?",
answers: ["Light Blue", "Brown", "Yellow", "Green"],
solution: "Yellow"
}
,
squirtle : {
question: "What color is Squirtle?",
answers: ["Blue", "Green", "Red", "Black"],
solution: "Blue"
}
}
// for in is for objects so use that here...
for (key in questionDictionary) {
var q = questionDictionary[key];
console.log(q.question) // the question
console.log(q.answers) // the answers
console.log(q.solution) // the solution
}
// lookup explicit question
var q = questionDictionary.squirtle;
console.log(q.question) // the question
console.log(q.answers) // the answers
console.log(q.solution) // the solution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment