简体   繁体   中英

How does this array work?

just following a simple javascript quiz. The code below works but I have a few questions about it.

var player = prompt("Hello, welcome to the quiz, what is your name?");
var score = 0;

var questions = [
    ["What is my name?", "Joe"],
    ["What is my age?", 27],
    ["What is my favourite sport?", "Football"],
    ["What is my job?", "Web Developer"],
    ["What is my eye color?", "Blue"]
];

function askQuestions(question) {
 var answer = prompt(question[0],'');
 if(answer === question[1]) {
     alert('Correct!')
 } else {
     alert('Wrong!');
 }
};

for(var i= 0; i < questions.length; i++) {
    askQuestions(questions[i]);
};

So the first thing I don't understand is in the askQuestions function the prompt has 'questions[0]'. I know we have a loop but this isn't part of the loop right? So surely this variable is just storing the first array in the questions variable.

The second argument is an empty string ''. Does this just store the answer? How does that work?

I understand rest just having problems with the function.

If someone could explain that would be great!

Cheers

var questions is an array of arrays of 2D, and each of those 2D-arrays are passed in the loop to askQuestion. Then, for example, When you are passing question[0] to askQuestion at the first loop, you're really passing object ['What is my name','Joe']

Inside function askQuestion you'll pick up then first element of parameter array (so, 'What is my name'), and compare answer with second one element of parameter array (so, 'Joe')

Perhaps if you wrote askQuestion this way you will understand more the workflow :

function askQuestions(**paramArray**) {
  var answer = prompt(**paramArray**[0],'');
  if(answer === **paramArray**[1]) {
    alert('Correct!')
  } else {
    alert('Wrong!');
  }
};

About prompt, second parameter it's just the default text to show.

So the best way to see whats happening, just take the variable, and plug it in yourself.

askQuestions(questions[ i ])

plug in i:

askQuestions( questions[0] )

plug in questions[0]

askQuestions( ["What is my name?", "Joe"] )

plug in the function

function askQuestions(["What is my name?", "Joe"]) {
 var answer = prompt("What is my name?",'');
 if(answer === "Joe") {
     alert('Correct!')
 } else {
     alert('Wrong!');
 }
};

i commented the code here:

//player inputs their name and it is stored in var player
var player = prompt("Hello, welcome to the quiz, what is your name?");
//set var score to 0;
var score = 0;

//questions is an array, of arrays. so questions[0] is ["What is my name?", "Joe"] and questions[0][0] is "What is my name?" 
var questions = [
["What is my name?", "Joe"],
["What is my age?", 27],
["What is my favourite sport?", "Football"],
["What is my job?", "Web Developer"],
["What is my eye color?", "Blue"]
];

//askQuestions is a function that will be called in the for loop below
function askQuestions(question) {
 //set var answer to whatever the user types in, pop up will say whatever question[0] is Or in this case question[i][0] because we pass in question[i]
 var answer = prompt(question[0],'');
 //here we just check if what they type in is the same as the answer we expect
 if(answer === question[1]) {
 alert('Correct!')
 } else {
     alert('Wrong!');
 }
};

//this loop is going to to run for the questions.length, in this case 5 times, and each time it will call askQuestions(question[0]) then askQuestions(question[1]) etc
for(var i= 0; i < questions.length; i++) {
   askQuestions(questions[i]);
};

Hope that helps... :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM