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 trialIvana Lescesen
19,442 PointsWhy is the else statement showing that there is no student if the rest of my code is correct? Thank you :)
var students = [
{
name: 'Dave',
track: 'Front End Development',
achievements: 158,
points: 14730
},
{
name: 'Jody',
track: 'iOS Development with Swift',
achievements: '175',
points: '16375'
},
{
name: 'Jordan',
track: 'PHP Development',
achievements: '55',
points: '2025'
},
{
name: 'John',
track: 'Learn WordPress',
achievements: '40',
points: '1950'
},
{
name: 'Trish',
track: 'Rails Development',
achievements: '5',
points: '350'
}
];
var search;
var message = '';
var data;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
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++) {
data = students[i];
if (data.name === search){
message = studentListing( data );
print(message);
}
else {
print( search + ' is not a student');
}
}
}
function studentListing (data) {
var html = "<h2>Student: " + data.name + "</h2>";
html += "<h3>Track: " + data.track + "</h3>";
html += "<h3>Achievements: " + data.achievements + "</h3>";
html += "<h3>Points: " + data.points + "</h3>";
return html;
}
Thank you so much :)
7 Answers
Steven Parker
231,248 PointsJennifer fixed the spurious error message, but the code still will not handle multiple search results (probably because she was rushed).
To be able to show more than one result, you must accumulate them in the message ("+=
"), not replace ("=
") the message each time. You'll also need to do the same thing with the "not a student" message.
Here's a main loop replacement that does this, and I made use of the existing isFound variable for generating the result instead of building an array and using a second inner loop to display it.
while (true) {
search = prompt('Search student records: type a name [Jody] (or type "quit" to end)');
var isFound = false;
if (search === null || search.toLowerCase() === 'quit') {
break;
}
for (var i = 0; i < students.length; i++) {
data = students[i];
if (data.name.toLowerCase() === search.toLowerCase()) {
message += studentListing(data);
print(message);
isFound = true;
}
}
if (!isFound) {
message += search + ' is not a student';
print(message);
}
}
While I was in there, I also made the search tolerant of case differences.
Happy coding! -sp
Ivana Lescesen
19,442 PointsThank you Steven Parker you rock :) How can I find students with the same name? Would you please be so kind to explain you process :)
Jennifer Nordell
Treehouse TeacherSteven Parker Thanks! You're a lifesaver! I was trying to get out the door when I got the notification that someone had tagged me for this question. I had about 7 minutes to code and put out an answer LOL. Good job
Jennifer Nordell
Treehouse Teacheredited because of incorrect answer
Sorry, Ivana Lescesen. I really wanted to answer, but was in such a rush! But I see Steven did a great job, so I've removed this answer so as not to clog up this thread with unnecessary code
Ivana Lescesen
19,442 PointsThank you so much Jennifer, you are awesome. If you have time could you please explain this par of of your code
for (var i = 0; i < students.length; i++) {
data = students[i];
if(data.name === search) {
searchResults.push( data );
}
}
if(searchResults.length > 0 ) {
for(var i = 0; i < searchResults.length; i++) {
message = studentListing ( searchResults[i]);
print( message );
}
}
I did not know we could use two if statements together. I tried this but it did not work. You could have more than one "John" or more than one "Dave" in the students array. My code will print out all "Daves", should you have more than one
Thank you :)
Ivana Lescesen
19,442 PointsDo not worry :) Thanks :)
Ivana Lescesen
19,442 PointsDear Steven Parker How can I find students with the same name? Would you please be so kind to explain you process :) And is there any way for the result of the if (!isFound) Mark is not a student Angela is not a student Eric is not a student to be listed one below the other ?
Thank you so much, you truly wonderful for taking time to answer me :)
All the best :)
Ivana
Steven Parker
231,248 PointsThe loop that checks the names goes through every name, and for every match (whether one or more than one) the studentListing for that student is added to the message string. So if you have two students records with the name "Dave", one search for "dave" will find them both.
To create separation between the "not found" results, you could wrap them in a paragraph:
message += '<p>' + search + ' is not a student</p>';
Ivana Lescesen
19,442 PointsThank you :)
Ivana Lescesen
19,442 PointsDear @Steven Parker Jennifer Nordell We define var isFound = false; because no search has been made yet.
When our search is equal to a student we have we change the variable to true isFound = true;
In order to provide a message about the students that does not exist we have to set (!isFound) which is equal to not true
Is that correct?
Thank you in advance :)
Steven Parker
231,248 PointsI think you have it, but I might describe that last part this way:
The line "if (!isFound)
" doesn't set anything but it checks the value of isFound.
The exclamation point is the not operator, so with the expression "!isFound
" (literally "not isFound") the entire line means "if isFound is not true".
Ivana Lescesen
19,442 PointsThank you :)
Sonja Tomcic
6,590 PointsSteven Parker Thanks a lot!!!!!! I was trying to solve this for 3 days
Steven Parker
231,248 PointsIt's always a treat to find that I've helped more than one person with an answer.
I might have to get Jennifer to teach me how to organize references to old postings for re-use sometime.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherI'm looking now. And I have an idea. Using your current code, type in "Trish" in your search and see your result I'm working out the solution