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 trialjdee
6,655 PointsWhy does this code concat data to the browser with each new student name vs. only display one student's data ?
In the video, the instructor's code only displays the data for one student, if found. When he enters a second name, the screen clears and results are show also for only one student. I cannot make that happen with the code below (a slightly modified version of the solution).
Please help!
var nameFromUser = ''; var message = '';
function returnReport(student) { var report = '<h2>Name: '+ student.name + '</h2>'; report += '<p>Track: '+ student.track + '</p>'; report += '<p>Achievements: '+ student.achievements + '</p>'; report += '<p>Points: '+ student.points + '</p>'; return report; }
while (true) { nameFromUser = prompt('Provide name or \'quit\' to end: '); nameFromUser = nameFromUser.toLowerCase();
if(nameFromUser === 'quit') { break; }
for( var i=0; i < students.length; i+=1) { if(students[i].name.toLowerCase() === nameFromUser) { message = returnReport(students[i]); document.write(message); } } }
2 Answers
KRIS NIKOLAISEN
54,971 PointsFrom the teacher's notes for the video
Important Update Since this video was shot, the behavior of most browsers has changed, so you won't see the same thing as I demonstrate in the video. In the video, you'll see that my script is able to print out to the browser using document.write( ) while inside a loop.
Most browsers no longer do that: they wait until the loop finishes and then they print to the window. So, you'll see a blank page until you type quit in the prompt window — then you'll see all the output printed to the screen.
jdee
6,655 PointsThanks. I'll read teach notes more closely. And sorry for the bad code formatting!