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 trialPeter Ajuzie
Full Stack JavaScript Techdegree Student 2,719 PointsMy if statement did not work for when student was not found. Please help.
Please help me figure out why the if statement (i commented out near the bottom) did not work for when my student was not found. I actually returned not found for all searches :(
Thanks in advance!
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 ( search.toLowerCase() === student.name.toLowerCase() ) {
message += getStudentReport( student );
print(message);
}
//THIS WORKED BUT THE ONE BELOW IT DID NOT WORK. Why?
if ( message == '' ) {
print('Student <i>' + search + '</i> was not found.');
}
// THIS DID NOT WORK BUT THE ABOVE "if (message == '')" worked. Why??
// if ( search.toLowerCase() !== student.name.toLowerCase() ) {
// print('Student <i>' + search + '</i> was not found.');
// }
}
}
1 Answer
Neil McPartlin
14,662 PointsHi Peter,
Your troubled 2nd entry will work, but only when searching for 'Trish' (the last entry in the students object), I'll try and explain.
The students object currently comprises 5 students. If we search for Dave who is at index 0, your for loop will check the 1st if statement and will find a match and populate the 'message' variable. The 2nd if statement will not match BUT the loop continues and will check indexes 1 thru 4 meaning that during this final loop, your 2nd if statement is correct (Dave does not match Trish) and we get the erroneous message that 'Student Dave was not found'. Hopefully you can see that if instead you had searched for Trish, her records are returned.
So your solution of using ( message == '' ) is better because the response to your 2nd if statement during the final loop is the one that matters, when intending to report that no match was found.
Peter Ajuzie
Full Stack JavaScript Techdegree Student 2,719 PointsPeter Ajuzie
Full Stack JavaScript Techdegree Student 2,719 PointsThanks, Neil!