Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    12
    Thanked 0 Times in 0 Posts

    problem with function parameter

    Hi,
    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?


    Code:
    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]) {
    alert('Correct!');
    score++;
    } else {
    alert('Sorry. The correct answer is ' + question[1]);
    }
    	}
    for (i=0; i<questions.length; i++) {
    	askQuestion(questions[i]);
    	}

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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
    Code:
    var firstInnerArray = questions[0];
    To access the first element of the first inner array, you can do this
    Code:
    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.
    Last edited by devnull69; 03-16-2012 at 03:11 PM.

  • Users who have thanked devnull69 for this post:

    guermantes (03-16-2012)

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi,
    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?

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •