View Full Version : problem with function parameter

03-16-2012, 02:59 PM
I am learning javascript from a book ("Javascript and JQuery Missing Manual") and one example there sets my head spinning and I can't understand how the script works. I'd be very grateful if someone could write a few words about the aspect I don't understand.

Basically it is line seven I don't understand. How is the word question tied to the items in the questions array. I understand the 0 and 1 on line eight and nine and why they focus on the first and second item in the sub-array, but still, how can the word question point to the relevant item in the questions array?

var score = 0;
var questions = [
['How many moons does Earth have?', 1],
['How many moons does Saturn have?', 31],
['How many moons does Venus have?', 0]
function askQuestion(question) {
var answer = prompt(question[0], '');
if (answer == question[1]) {
} else {
alert('Sorry. The correct answer is ' + question[1]);
for (i=0; i<questions.length; i++) {

03-16-2012, 03:09 PM
Ok, what you have here is a two-dimensional array, which is an array of arrays. Each element of the outer array is itself an array with two elements. To access the first inner array of the outer array, you can do

var firstInnerArray = questions[0];

To access the first element of the first inner array, you can do this

var firstElement = firstInnerArray[0];

// or this, which is equivalent
var alsoFirstElement = questions[0][0];

In your case, the three bottom lines will iterate over each element of the outer array. Each resulting element questions[i] will be handed over to the function askQuestion as parameter "question". As I've just shown you, each element questions[i] is itself an array, so as a result the parameter "question" of the function askQuestion will also be an array.

So what you get by accessing question[0] is in fact questions[0][0] for the first iteration or questions[1][0] for the second iteration and so on.

03-16-2012, 03:57 PM
Thanks for your reply!

So I had the process inside-out when I thought that by using the word question one was accessing items in the array? It's actually the other way around? The word question is just a variable that is filled with whatever we fill it with, in this case the parameter from when the function is called ?

So, in the first iteration, calling the function askQuestion (questions[i]) is the same as askQuestion (['How many moons does Earth have?', 1]), and when the function executes the word question is filled with ['How many moons does Earth have?', 1] ?

I thought somehow that the word question would go and fetch the correct value in the questions array, but it is actually big filled by what is passed to it.

Would that be a correct way of looking at it, even though my terminology may be wrong?

03-16-2012, 08:26 PM
Yes, you are absolutely right.

The variable names "question" and "questions" are two completely separate variables. The programmer has chosen the similar names because he/she wanted to make clear that "question" holds one entry of the "questions" array. There is no implicit or automatic semantics in Javascript between two different variables as long as their name is different. Even "QuEsTiOn" would be a different variable from "question".

And yes, askQuestion(questions[i]) is the same as askQuestion(['How many moons does Earth have?', 1]) for i==0