1

this is an array that contains the question on index 0 and choices from 1-4 and then on index 5 contains the answer. I want to access these index numbers individually but cant.

questionbank = [
  ["Which city is the capital of Pakistan", "Islamabad", "Karachi", "Lahore", "Quetta", "Islamabad"],
  ["Which city is the capital of U.A.E", "Dubai", "Abu Dhabi", "Sharjah", "Ras Al-Khaimah", "Dubai"],
  ["Which city is the capital of United States of America", "Austin", "Washington DC", "Boston", "Colorado", "Washington DC"],
  ["Which city is the capital of UK", "London", "Manchester", "Leeds", "Sunderland", "London"],
  ["Which city is the capital of Kuwait", "Kuwait City", "Salmiya", "Jahra", "Tokyo", "Kuwait City"],
  ["Which city is the capital of Saudi Arabia", "Riyadh", "Mecca", "Madinah", "Jeddah", "Riyadh"],
  ["Which city is the capital of India", "Mumbai", "Delhi", "Bareilly", "Calcutta", "Mumbai"],
  ["Which city is the capital of Afghanistan", "Kabul", "Kandahar", "Herat", "Jalalabad", "Kabul"],
  ["Which city is the capital of Ireland", "Dublin", "Belfast", "Cork", "Limerick", "Dublin"]
];

function next_question() {
  text = questionbank[0, 0];
  alert(text);
}

next_question();

The problem is when i call [0,0] the whole sentence in the array comes up instead of just one element in the array. My array is a collection of questions on position 0 and the collection of choices in the other positions. Can you please tell me how to access only one element of the array at a time!

5 Answers 5

1

You're not using the correct syntax to access the second dimension. [0, 0] is equivalent to [0]. The comma doesn't separate dimensions, it's the comma operator that simply evaluates both operands and returns the second one. A multi-dimensional array is an array of arrays, so you access each dimension with a separate set of brackets: arrayname[subscript1][subscript2]

questionbank = [
  ["Which city is the capital of Pakistan", "Islamabad", "Karachi", "Lahore", "Quetta", "Islamabad"],
  ["Which city is the capital of U.A.E", "Dubai", "Abu Dhabi", "Sharjah", "Ras Al-Khaimah", "Dubai"],
  ["Which city is the capital of United States of America", "Austin", "Washington DC", "Boston", "Colorado", "Washington DC"],
  ["Which city is the capital of UK", "London", "Manchester", "Leeds", "Sunderland", "London"],
  ["Which city is the capital of Kuwait", "Kuwait City", "Salmiya", "Jahra", "Tokyo", "Kuwait City"],
  ["Which city is the capital of Saudi Arabia", "Riyadh", "Mecca", "Madinah", "Jeddah", "Riyadh"],
  ["Which city is the capital of India", "Mumbai", "Delhi", "Bareilly", "Calcutta", "Mumbai"],
  ["Which city is the capital of Afghanistan", "Kabul", "Kandahar", "Herat", "Jalalabad", "Kabul"],
  ["Which city is the capital of Ireland", "Dublin", "Belfast", "Cork", "Limerick", "Dublin"]
];

function next_question() {
  text = questionbank[0][0];
  alert(text);
}

next_question();

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! you're awesome :)
Nice explanation about the comma operator.
0

To access to two-dimensional array you should use this notation [0][0]:

questionbank = [
  ["Which city is the capital of Pakistan", "Islamabad", "Karachi", "Lahore", "Quetta", "Islamabad"],
  ["Which city is the capital of U.A.E", "Dubai", "Abu Dhabi", "Sharjah", "Ras Al-Khaimah", "Dubai"],
  ["Which city is the capital of United States of America", "Austin", "Washington DC", "Boston", "Colorado", "Washington DC"],
  ["Which city is the capital of UK", "London", "Manchester", "Leeds", "Sunderland", "London"],
  ["Which city is the capital of Kuwait", "Kuwait City", "Salmiya", "Jahra", "Tokyo", "Kuwait City"],
  ["Which city is the capital of Saudi Arabia", "Riyadh", "Mecca", "Madinah", "Jeddah", "Riyadh"],
  ["Which city is the capital of India", "Mumbai", "Delhi", "Bareilly", "Calcutta", "Mumbai"],
  ["Which city is the capital of Afghanistan", "Kabul", "Kandahar", "Herat", "Jalalabad", "Kabul"],
  ["Which city is the capital of Ireland", "Dublin", "Belfast", "Cork", "Limerick", "Dublin"]
];

function next_question() {
  text = questionbank[0][0];
  alert(text);
}

next_question();

1 Comment

Thanks! you're awesome :)
0

Use the standard notation [dimension][anotherDimension]... in your case [0][0] instead of [0,0]. The last one is not valid in terms of JavaScript or at least does not do what you want. You might have a different background, like C#, where it is a valid multidimensional notation.

Comments

0

This is not questionbank[0, 0] but questionbank[0][0].

1 Comment

Thanks! you're awesome :)
0

This looks like a 2d array so you should access like:

questionbank[0][0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.