JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

JAMES RODRIGUEZ
JAMES RODRIGUEZ
856 Points

What I am I doing wrong, I keep getting a "TypeError Cannot read property 'someStr' of undefined."

Build Quiz Challenge- I keep getting a "TypeError Cannot read property 'someStr' of undefined." I've looked over everything and I can't figure it out, I've been working on this project now for a week trying to figure out my error. If I cycle through my questions using one array for questions and another for the answers, the questions loop works, however using two arrays (one for each Q&As) is kind of tedious when I still need to write code that will grade the quiz at the end.

Here's my code, and this is for another training program I'm in. So you'll notice that its strictly JavaScript with none of the extra HTML or CSS stuff like in the video, since we haven't gotten there just yet.

const input = require("readline-sync");

let canAnswers; let quizQuest; let correctAnswers;

let questions = [ ["1) True or False: 5000 meters = 5 kilometers ", "True"] ["2) (5 + 3) / 2 * 10 = ? ", "40"] ['3) Given the array [8, "Orbit", "Trajectory", 45], what is at index 2? ', "trajectory"] ["4) Who was the first American woman in space? ", "sally ride"] ["5) What is the minimum crew size for the International Space Station (ISS)? ", "3"] ]

for (let i = 0; i < questions.length; i++) { quizQuest = questions[i][0]; correctAnswers = questions[i][1]; canAnswers = input.question(questions); canAnswers = canAnswers.toLowerCase(canAnswers) }

It seems to crash on the third question's answer. If I just have the first two questions and comment out the other 3 it runs no problem. Any help would be highly appreciated thanks.

2 Answers

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,633 Points

I ran your code and I got a different, but similar, error:

TypeError: Cannot read property 'trajectory' of undefined

I don't see someStr anywhere, was that a different version?

At any rate, one problem I see is that you have a questions variable which is an array of arrays. But there aren't commas in between each inner array. Should maybe look like this:

let questions = [ 
    ["1) True or False: 5000 meters = 5 kilometers ", "True"],
    ["2) (5 + 3) / 2 * 10 = ? ", "40"],
    ['3) Given the array [8, "Orbit", "Trajectory", 45], what is at index 2? ', "trajectory"],
    ["4) Who was the first American woman in space? ", "sally ride"],
    ["5) What is the minimum crew size for the International Space Station (ISS)? ", "3"] 
]
JAMES RODRIGUEZ
JAMES RODRIGUEZ
856 Points

nope it's just what I use when searching the web for a solution and it was still on my clipboard. the error you got is exactly what I get

JAMES RODRIGUEZ
JAMES RODRIGUEZ
856 Points

Thanks Brendan for spotting the missing commas. My other issue was for some reason I had to add other for index loop for the second column. the for loop should have looked like this

for (let i = 0; i < questions.length; i++) { for (let j = 1; j < questions[i].length; j++) {

With that one extra line it's now looping through all of my 5 questions and not yelling at me. Not exactly sure why I had to add that in there still, I figured it out just playing around with it. So if someone can still explain why, unlike everyone else, I had to add this extra line of code it would greatly appreciated.