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 1

Yemaya Re
PLUS
Yemaya Re
Courses Plus Student 1,922 Points

Botching this 'simple' quiz... Please Help

Hey team, I am so lost as to how to proceed. I don't know what to do. My code below isn't working as it should and I'm unsure how to break this down into smaller chunks to build and execute. If you could maybe refrain from giving me the answer in code and instead provide hints/direction on what train of thought/ questions I should ask myself so I'm able to break this down into small chunks, because I'm super lost right now. I think the only part I got down so far is the two dimensional array, so what should I focus on after that? I hope I made sense...Thanks so much!

var quiz = [

[ 'Name the law that tells us that everything is connected to everything else. This is the first Law of the Universe.', 'The Law of Divine Oneness.' ],

[ 'This law states that everything in creation carries its own unique frequency. The phrase "Like attracts like" refers to this second Universal Law.', 'The Law of Vibration' ]

[ 'This law states that we must engage in actions that support our thoughts, words, feelings, vision, beliefs and dreams. This is the third Universal Law.', 'The Law of Action' ]

];

function question(quiz) {

for(var i = 0; i <= quiz.length; i += 1 ) {

prompt(quiz[0]);

} }

function print(message) { document.write(message); }

print(question(quiz[0][]));

Todd Anderson
Todd Anderson
4,260 Points

My first question is can you share the error code?

2 Answers

Steven Parker
Steven Parker
229,786 Points

Here's some hints:

  • don't group items in an array with parentheses
  • always put a comma between items in an array
  • when a loop starts at 0, it should go up to (but not include) the length of the iterable
  • if you supply a literal number (like 0) as an index, you'll always get the same (first) item
  • calling "prompt" without assigning the result asks the question but doesn't store the answer anywhere
  • the question function is expecting the whole array so don't index the argument when you call it
  • an empty index ([]) causes a syntax error
Yemaya Re
Yemaya Re
Courses Plus Student 1,922 Points

Hey Steven, thank you for your guidance! I did what I could and I'm at the point where it prompts everything within the main array. How can I go about summoning just one question and not all questions or the answer?

Steven Parker
Steven Parker
229,786 Points

For just one question, instead of using a loop, just call "prompt" and index using the question number you want. And to get only the question, use 0 as the second index:

answer = prompt(quiz[question_number][0]);
Erwan EL
Erwan EL
7,669 Points

I think you need to declare much more variables to detail and specify every elements and actions it's going to be a lot easier for you to understand the code in every step, also in the for loop doesn't use <= prefer only <