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

HALP! ("Dave" works fine but "Jody" crashes the browser)

Here is a snapshot of my workspace:

https://w.trhou.se/d6ky28jyol

Typing "Dave" into the prompt works fine and prints to the document, but anything else seems to crash the browser. Also, I need to add an alert that lets the user know when they enter an invalid input. I've checked out the solutions that actually work, and I sorta get it, but I'd like an explanation of specifically why this code doesn't work, and what to do to repair it, if possible. Thanks!

1 Answer

Hi!!

In your "getStudentReport" function, you receive a student as argument, but immediately after the function starts, the line "student = studentNames[i];" replaces the contents of your student, and it no longer holds the value of the argument. Also, the variable i is not defined inside this function. Since you already used the "student" variable as an argument, if you don't replace its content, you can just use student.name, student.track and so on, instead of "studentNames[i].name"

Besides that, since your "return" statement is inside the loop, it will only execute once, no matter the length of studentNames. What I think you were trying to accomplish is to print the report of only one specific student in this function, so you don't really need a loop there. The loop goes in the outside function already.

Hope it helps a bit!

Thanks so much! After making your suggested changes it was really simple to get everything working the way I wanted it to.