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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

wentian zhou
wentian zhou
2,104 Points

I dont get why I cant print out anyones name

i copied the video code for code

var message = ''; var student; var search;

function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message; }

function getStudentReport( student ) { var report = '<h2>Student: ' + student.name + '</h2>'; report += '<p>Track: ' + student.track + '</p>'; report += '<p>Points: ' + student.points + '</p>'; report += '<p>Achievements: ' + student.achievements + '</p>'; return report; }

while (true) { search = prompt('Search student records: type a name [Jody] (or type "quit" to end)'); if (search === null || search.toLowerCase() === 'quit') { break; }

for (var i = 0; i < students.length; i += 1) { student = students[i];

if ( student.name === search) { message = getStudentReport( student ); print(message); } }

}

Kristaps Vecvagars
Kristaps Vecvagars
6,193 Points

Does it print to the document, when you cancel out or 'quit' out of the loop? I suspect it might be browser behavior or something else, because my solution was practically identical to Dave's and it wouldn't print anything during the loop. Even when I copied Dave's code, it would work the same. As soon as I would 'quit' or cancel the loop, the page view would update as expected. In other words, the print(message) function seems to run just fine, but the screen doesn't update, while the endless prompt loop is still running.

7 Answers

Kristaps Vecvagars
Kristaps Vecvagars
6,193 Points

wentian zhou Your code works just fine, same as mine. The only difference comparing to the video is that the script no longer prints results during an active loop. This is normal. As someone already pointed out to me, it was covered in the teacher's notes.

If your script does not print anything AFTER 'quit', well, then perhaps there is something wrong with your students list or something isn't linked correctly, or something along those lines. I copied your code and it worked perfectly in my workspace.

Steven Parker
Steven Parker
231,268 Points

Kristaps makes a good point, but it was also given on the page.

Look down under the video at the Teacher's Notes section of the page. Under the heading Important Update you will find a description that will likely explain what you are experiencing.

wentian zhou
wentian zhou
2,104 Points

I tried using sublime text and opening on another browser and the alert prompt pops out, but never prints anything. same thing with treehouse workspace

Steven Parker
Steven Parker
231,268 Points

After including the array code, the program performs as expected — as previously mentioned the output is only seen after you "quit".

If you don't get that behavior, perhaps you should make a snapshot of your workspace and post the link to it here so we can replicate your environment exactly.

wentian zhou
wentian zhou
2,104 Points

var students = [ { name: 'Dave', track: 'Front End Development', achievements: '158', points: '14730' }, { name: 'Jody', track: 'iOS Development with Swift', achievements: '175', points: '16375' }, { name: 'Jordan', track: 'PHP Development', achievements: '55', points: '2025' }, { name: 'John', track: 'Learn WordPress', achievements: '40', points: '1950' }, { name: 'Trish', track: 'Rails Development', achievements: '5', points: '350' } ];

wentian zhou
wentian zhou
2,104 Points

Are you able to see my snapshots?

Steven Parker
Steven Parker
231,268 Points

When you make a snapshot, it will give you a link address to post here. The link will allow us to see.

The procedure is covered in the video I linked to earlier (click the blue word).

Steven Parker
Steven Parker
231,268 Points

If I type a student name, nothing happens, but then when I type "quit" I see the student record. That's exactly what I would expect it to do.

wentian zhou
wentian zhou
2,104 Points

var students = [ { name: 'Dave', track: 'Front End Development', achievements: '158', points: '14730' }, { name: 'Jody', track: 'iOS Development with Swift', achievements: '175', points: '16375' }, { name: 'Jordan', track: 'PHP Development', achievements: '55', points: '2025' }, { name: 'John', track: 'Learn WordPress', achievements: '40', points: '1950' }, { name: 'Trish', track: 'Rails Development', achievements: '5', points: '350' } ];

Steven Parker
Steven Parker
231,268 Points

Posting as an "answer" instead of a comment caused it to appear out of sequence. But see the comment I added to m answer.