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 trialH Yang
2,066 Pointsif/else branches
I'm trying to have the browser respond that a student is not in a class with an else clause, but it's causing my code to not return any information for students in the roster.
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 ("Please input the student's name. To quit, type 'QUIT'.");
if (search === null || search.toUpperCase() === "QUIT") {
break;
}
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (student.name === search) {
message = getStudentReport (student);
print (message);
} else {
print (search + " is not a student here.");
}
}
}
What's wrong with my logic branch? I thought there were three possibilities- a null/quit, student in the roster, and a student who's not in the roster. Why isn't the else branch working correctly? Thanks.
3 Answers
Sheryl Darroch
Treehouse Project ReviewerHi Henry!
Almost there. Try changing the () after your else to {}.
Don't give up!
Craig Garner
25,732 PointsYour if statement is wrong, No batter what, the last loop will run. break !== exit
H Yang
2,066 PointsWould you mind specifying what's wrong with the if statement? When I run my code without the last two lines (i.e. without the else statement) it runs.
Sheryl Darroch
Treehouse Project ReviewerHi Henry,
Sorry, I was reading on a small screen, originally.
Try this:
while (true) { search = prompt("Search student records: type a name 'Sheryl' or type 'quit' to exit."); if (search === null || search.toLowerCase() === 'quit') { break; }
for (var i = 0; i < students.length; i += 1) { student = students[i]; if (student.name.toLowerCase() === search.toLowerCase()) { message += getStudentReport( student );
}
}
if(message === '') { message = 'No records matching student name: ' + search; } print(message); }
H Yang
2,066 PointsSheryl,
I see how you built the branches and how it works. Thanks a lot!
Are you able to hazard a guess as to why my else branch didn't work as typed? I accounted for null, for a hit on the records- I thought that the only possibility left was for no result, and all of that would funnel into the else. But coding the way I did broke the working part (i.e. Finding and displaying an existing student).
Thanks again for your time.
H Yang
2,066 PointsH Yang
2,066 PointsHi Sheryl,
Excuse me, I believe those are brackets. I just checked it again and it's not working. Unless you're referring to the ones after "print"?
Thanks for the help.