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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

Marcos Raj
PLUS
Marcos Raj
Courses Plus Student 5,979 Points

Please help me with the code?

var kbc = [ ['What is part of a database that holds only one type of information?', 'Field'], ['"OS" computer abbreviation usually means ?', 'Operating System'], ['In which decade with the first transatlantic radio broadcast occur?', 'Animation/movie file'] ];

var options = [ ['Report', 'Field', 'File', 'Record'], ['Order of Significance', 'Open Software', 'Operating System', 'Optical Sensor'], ['1850', '1860','1870','1900'] ];

var html; function print(message) { document.getElementById('output').innerHTML = message; }

function questionArray(arr) { var list = '<ol>'; for(var i = 0; i < arr.length; i += 1) { list += '<li>' + arr[i][0] + '</li>'; for( var j = 0; j < options.length; j += 1) { for( var k = 0; k < 4; k += 1) { list += '<li>' + options[j][k] + '</li>'; } } } list += '</ol>'; return list; }

html = '<h1> Questions </h1>'; html += questionArray(kbc) print(html);

I want answers, all four options print under the respective questions? Where and what code shall i need to add or change.

Steven Parker
Steven Parker
231,275 Points

Please use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

1 Answer

Adrien Contee
Adrien Contee
4,875 Points

What you need can be done by adding an additional for loop that loops through the multiple choice answers and displays them under the question. Create a 2 dimensional array housing and the multiple choice answers.

For example:

var quiz = [
    ["How many states are in the USA?", "50"],
    ["What programming language is named after a gem?", "Ruby"]
];

var multipleChoice = [
    ["48", "49", "50", "51"],
    ["Perl", "Python", "Ruby", "Java"]
];

That should get you started and heading in the right direction. Use the skills you've learned from this lesson to build the rest.

Currently it would not work the way you need it to without adding DOM manipulation elements and event listeners. If you tried to do it without it either the prompt would load before the questions had a chance to display or the prompt would load after covering up the quiz.

If you didn't care too much about the questions popping up in a prompt dialog box. Then you could create your quiz easily using a simple HTML Form using radio buttons.

Hope that helps. Good luck.

Marcos Raj
Marcos Raj
Courses Plus Student 5,979 Points

Thank you, I have got the solution, i used DOM to execute the same.