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 trialIsabella Tan Hui Huang
3,577 PointsHelp! Code returns 'undefined' for property value and doesn't display data at the end of each loop.
Can someone help me? For some reason my code is returning 'undefined' for property values (instead of the values for name, track, achievements and points), and it only shows the data after I type 'quit'. I simply don't get it...
2 Answers
Frankie Libbon
4,603 PointsHi Isabella!
For your problem with showing undefined
instead of the property values, I would take a closer look at your getReport
function in students.js. You're passing in a student
parameter, but then using students
to get your values, which means the function never uses student
As for your program only showing data after you write "quit," I would look at your while
loop and what is currently breaking you out of it, which right now is only lines 29-31. Instead of printing inside the for
loop, you could save the data to that studentData
variable you have and check outside of the loop if the studentData
variable is set to anything. If it does, you can print and break out of the while loop there as well!
Good luck :)
miikis
44,957 PointsHi Isabella,
Just took a quick look but it looks like, in your getReport() function, you have a parameter named student but βinside of the function β you're using a variable called students instead. Fix that and it should work as you expect :)
miikis
44,957 Pointsmiikis
44,957 PointsThe second part of this is incorrect; there's a loop inside a loop here and that second loop β the for loop β only runs for each member of students. So she's fine doing it the way she did it.
Frankie Libbon
4,603 PointsFrankie Libbon
4,603 PointsYup, the for loop is working fine and will print out exactly what she needs. I was just trying to give direction to resolve "and it only shows the data after I type 'quit'"
If she saves the found data from the for loop in
studentData
and prints it out of the loop, then saysbreak;
, the infinite while loop will stop and display the data for that student instead popping the dialog up again and again until "quit" is typed in.miikis
44,957 Pointsmiikis
44,957 PointsOh, I see what you were saying. See, I was thinking Isabella wanted the endless while loop so that she could append students to the output div as they were searched and found. But, I took another look at print() and she's overwriting the contents of the div each time, so maybe I'm wrong ;) Even so, though, Frankie, there's no need to save the data in studentData; she can just print(getReport(studentIndex)) and then call break.
Frankie Libbon
4,603 PointsFrankie Libbon
4,603 PointsHaha yup! I'm not familiar with this challenge so I'm not sure if the end goal is to keep the dialog popping up, but it seems strange so I figured that could fix it. Also, unfortunately if she just writes
break;
after the print as it's called now, thatbreak;
is only in the context of thefor
loop and won't break her out of the while loop! Hence why I suggested saving the data and printing/breaking after thefor
loop :)miikis
44,957 Pointsmiikis
44,957 PointsLol you're so right! Idk what I was thinking... just being a dum-dum. I up-voted you ;)
Frankie Libbon
4,603 PointsFrankie Libbon
4,603 PointsThanks Mikis :D And don't worry about it, I love a good coding discussion!
Isabella Tan Hui Huang
3,577 PointsIsabella Tan Hui Huang
3,577 PointsThanks Frankie, I fixed the function and that works. For the second part, your answer is the best solution, although in the video Dave doesn't break out of the while loop and still gets the browser to print the data. Looks like other students are also unable to replicate his results...odd... Anyway, thanks! :)