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

The prompt box doesnt run - The Student record challenge

Hi, this is my code, even I copy from lecture but it doesnt work, please help me

var message = ''; var student; var ask; function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message; } function getstudent(student) { var result += '<h2>Student: ' + student.name + '</h2>'; result += '<p>Track: ' + student.track + '</p>'; result += '<p>Points: ' + student.points + '</p>'; result += '<p>Achievements: ' + student.achievements + '</p>'; } while(true){ ask = prompt('Who are you looking for?'); if(ask===null || ask.toLowerCase ==='quit'){ break; } for (var i = 0; i < students.length; i ++) { student = students[i]; if(student.name === ask){ message = getstudent(student); print(message); } } }

1 Answer

Steven Parker
Steven Parker
243,656 Points

It's difficult to read the unformatted code, but one thing caught my attention. In the function getstudent you have this line:

  var result += '<h2>Student: ' + student.name + '</h2>';

You probably meant to write "=" instead of "+=" here, since you cannot add to a variable that is just being created.
The other code should function once that error is corrected.

In future, to facilitate a complete and accurate answer, always format your code (see the Markdown Cheatsheet) and include a link to the course page you are working with.