Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

[Solved] How to nest a 2 dimensional array into another array?

I'm not sure if this is the most efficient solution, but its close to working. Bottom is a code snippet...

Basically

myQuestion[rQuest[i]] prints out the whole question array i.e 'What is the capital of Britain, london'

I want to just print the first part ie. 'What is the capital of Britain'

I tried myQuestion[rQuest[i] ,0] to specify the question part but its not working.

What am I doing wrong?

var myQuestion = [
  [['What is the capital of Britain'],['london']],
  [['What is the capital of France'], ['paris']],
  [['What is the capital of Germany'], ['berlin']]
];

var ans;
var rQuest =[];

for (var i = 0; i < myQuestion.length; i += 1) {
     ans = prompt(myQuestion[i][0]);
        if (ans == myQuestion[i][1]) {
        rQuest.push(i);
        console.log(rQuest);
     } else {
     }
}

  // Right Questions Printout loop

function printQuestr( questions ) {
  var listHTML = '<ol>';
    for ( var i = 0; i < questions.length; i += 1) {
        listHTML += '<li>' + myQuestion[rQuest[i]] + '</li>';
  }
    listHTML += '</ol>';
  print(listHTML);
}

printQuestr(rQuest);

//Fixed Code Presentation

1 Answer

Thanks, I found the answer.

myQuestion [rQuest[i]] [0]