Last active
January 26, 2018 15:56
-
-
Save plusjade/6160994 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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