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

brendon hanson
brendon hanson
6,191 Points

code isn't working

var students = [ { name: 'Brian', Acheivements: 4, Points: 257, Track: 'IOS' }, { name: 'Linda', Acheivements: 7, Points: 399, Track: 'Android' }, { name: 'Chester', Acheivements: 5, Points: 150, Track: 'JS' }, { name: 'Felisha', Acheivements: 9, Points: 990, Track: 'Python' }, { name: 'Luke', Acheivements: 1, Points: 780, Track: 'Ruby' }
];

var message = ' ';

var student;

var question;

//Custom Function that replaces document.write with a better alternative function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message; } // These are the student reports function getReport( student ) { var report = '<h2> Student: ' + student.name + '</h2>'; report += '<p> Track: ' + student.Track + '</p>'; report += '<p> Acheivements: ' + student.Acheivements + '</p>'; report += '<p> Points: ' + student.Points + '</p>'; return report; }

while (true) {

question = prompt("Would you like to see our student records? Type in any name to find their record or type Quit to exit");

if (question === null || question.toLowerCase() === 'quit') { break;

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

  message = getReport( student );
  print(message);

}

} }

print(message);

brendon hanson
brendon hanson
6,191 Points

Ignore the variables on top

Joseph Wasden
Joseph Wasden
20,406 Points

Tough to say. How should the code be working? What do you expect it to do? what is it doing instead? Are you working with a data set of some kind? Consider including some data mock up, if so. Also, consider including the accompanying HTML, and consider reformatting the code in question. As it stands, it is difficult to read.

I can only say it looks like you are missing a closing }, but I'm not sure if that just got lost in the cut/paste or not. Good luck!

1 Answer

Steven Parker
Steven Parker
229,608 Points

As explained in the Teacher's Notes, the browser will not render any output until the loop ends (you type "quit" or click on "cancel"). If you were expecting to see output as you typed that might be the issue.

Otherwise, please explain in more detail.

Also, when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

brendon hanson
brendon hanson
6,191 Points

I fixed it up a bit and I know about the browser thing this is a different issue...can you see any problems?

Steven Parker
Steven Parker
229,608 Points

Please describe the issue you are experiencing.

brendon hanson
brendon hanson
6,191 Points

The prompt pops up but when I input something and close it there's nothing same for when I type quit it closes but there is nothing. It should show all of the reports when I type quit. Also when I type in someones name it should show their individual profile when i close the prompt

Steven Parker
Steven Parker
229,608 Points

It works for me. I enter a name (note that it must be an exact match, including upper and lower case) and then type "quit" (or click "cancel") and I see the record on the page.

I did have to add this bit of HTML to the page first:

<div id="output"></div>
brendon hanson
brendon hanson
6,191 Points

I feel really STUPID! I didn't type in exact match! I wasted 6 hours on trying to figure out what was wrong. Thanks!

Steven Parker
Steven Parker
229,608 Points

Don't feel bad, mistakes are the best teachers. :wink:

If you want, you could make it case-insensitive like this:

    if (student.name.toLowerCase() === question.toLowerCase()) {

Anyway, glad to help. Happy coding!