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

Yemaya Re
PLUS
Yemaya Re
Courses Plus Student 1,922 Points

If student not in records

So I've moved the if statement around within the program to get it to message correctly if a student does not exist. At one point it was an if/else statement and I'm am lost as to where to place it or what I''m doing wrong.

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('Type student name to view records. 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); } }

} ---It's this code--> if (search !== student[i]) { message += 'There is no student by that name in our records.'; }

Steven Parker
Steven Parker
229,644 Points

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.

1 Answer

Steven Parker
Steven Parker
229,644 Points

The variable "i" is only useful inside the loop that searches the student names.

What you might do is create a boolean that is initially false, but if a student is found you could set it to true. Then, after the loop you could check it to see if the no-student message should be shown.

Yemaya Re
Yemaya Re
Courses Plus Student 1,922 Points

Ok I'll try that, and I was wondering how people posted their code correctly when asking a question so thanks for pointing that out!