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 trialUche Onuekwusi
18,070 PointsPlease HELP ME??
I copied this code from one of the correct ones posted in this forum. i cant quite understand the last IF statement which is:
if (student.name !== searchBox) {
searchBox = prompt( searchBox +" is not a student, try again. Search for student or
type quit to leave the prompt");
break
Please can someone explain to me why ?
the full code is hear
var message = [];
var student;
var searchBox;
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> Achievement: ' + student.achievement + '</p>';
report += '<p> Points: ' + student.points + '</p>';
return report;
}
while(true){
searchBox = prompt('Search for student or type quit to leave the prompt');
if( searchBox ===null ||searchBox.toLowerCase() === 'quit'){
break
}
for(var i =0; i< students.length; i++){
student = students[i];
if(student.name === searchBox){
message.push(getStudentReport(student));
}
if (student.name !== searchBox){
searchBox = prompt( searchBox +" is not a student, try again. Search for student or type quit to leave the prompt");
break
}else{
print(message);
}
}
}
3 Answers
Lee Vaughn
Treehouse Teacher!== is a comparison operator and it means "not equal to value or not equal to type".
In the context of your example, it would mean if student.name is not equal to searchBox then bring up a prompt that informs the user that what they entered in searchBox is not the name of a student.
Steven Parker
231,275 PointsIt may not be accurate to describe this code as "one of the correct ones". It looks like the statement you are asking about would cause premature termination of the loop, and the display of the error/prompt message even when a student is found. It also appears that the response to the prompt will be ignored, but another prompt will be issued no matter what is entered.
Be aware that in most cases, code posted with a question in the forum will have one or more errors, and the poster is seeking help in resolving them. This particular code example has a number of other issues that need fixing.
But the video you linked to contains the teacher's solution, which would be a much better model of correct code.
Iain Simmons
Treehouse Moderator 32,305 PointsSteven Parker is correct, the break
will exit the for
loop, not the while
loop, and will display two prompts.
You could instead change the second prompt
(within the for
loop) to just an alert
or similar, and then let it go back to the original prompt in the while
loop to ask for a new search term.
It looks like the for
loop should be allowed to finish, but you also want to set another variable to true
if you find at least one match, and then if you get to the end, and that variable is true, then display the results, otherwise, show an error and then allow the while loop to run again.