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 trialDenise Greer
5,026 Points'Else if' help in Student Record Search Challenge Solution
Ok so I went to add my else if in this challenge and now the prompt will not appear. What am I doing wrong?
JavaScript
var message = ''; var student; var search;
function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML = message; }
function getStudentRecord( student ) { var record = '<h2>Student: ' + student.name + '</h2>'; record += '<p>Track: ' + student.track + '</p>'; record += '<p>Points: ' + student.points + '</p>'; record += '<p>Achievements: ' + student.achievements + '</p>'; return record; }
while (true) {
search = prompt("Search for a student's name. Enter a name [John] 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 === search ) {
message = getStudentRecord( student );
print(message);
}
} else if ( student.indexOf( search ) > 1 ) {
print (student.join (', ') );
} else {
print( search + ' is not a student.');
}
}
1 Answer
Damien Watson
27,419 PointsHi Denise,
You have a bracket out of place with the second if statement before the else, and need to add one in at the bottom:
Current code:
while (true) {
search = prompt("Search for a student's name. Enter a name [John] 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 === search ) {
message = getStudentRecord( student );
print(message);
}
} else if ( student.indexOf( search ) > 1 ) {
print (student.join (', ') );
} else {
print( search + ' is not a student.');
}
}
Updated code:
while (true) {
search = prompt("Search for a student's name. Enter a name [John] 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 === search ) {
message = getStudentRecord( student );
print(message);
// removed }
} else if ( student.indexOf( search ) > 1 ) {
print (student.join (', ') );
} else {
print( search + ' is not a student.');
}
}
} // Added