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 triallevikordus
4,877 PointsI can't figure this out. Please help me :D
So, I'm not sure why this isn't working.
I've tried a lot of things, but i cant get this concept to work. The "solution" i coded seems like it should work.
levikordus
4,877 PointsSee the snapshot :)
Jennifer Nordell
Treehouse Teacherlevikordus Taking a look now!
2 Answers
Jennifer Nordell
Treehouse TeacherOkie dokie. I said I wasn't going to do this, but here we go! Keep in mind this was my solution. There are probably 100983 different ways to solve this.
var message = '';
var student;
var search = "";
var studentFound = false;
var tempIndex;
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.achievments + '</p>';
return report;
}
while (search !== 'quit') {
studentFound = false; //this is the line I just added
search = prompt("Search for a student record: type [Levi], type quit to exit, or press cancel.").toLowerCase();
for(var i = 0; i < students.length; i++)
{
student = students[i];
if (student.name.toLowerCase() === search){
tempIndex = i;
studentFound = true;
}
}
if (studentFound == true) {
message = getStudentReport(students[tempIndex]);
print(message);
} else {
alert("That student could not be found!");
}
}
You'll notice at the top that I made two new variables. One to record a temporary index, and one to hold our index in case we find a student. Now while this loop runs (ie while it's not quit), we are going to prompt our user for the student to search for. I went ahead and changed that answer to lower case right there so we don't keep having to type that over and over. Now I start my for loop and look through that array. If it is found, I make a note of the index where it was found by setting the tempIndex to that index. I also make a note that that person was found at all by setting studentFound to true.
Now, if the student was found... print their report. That part you did fine!
Otherwise, I say the student could not be found.
And I'm sure there are many out there that have more elegant solutions than mine, but this works. I hope it helps!
edited to fix my own bug LOL
levikordus
4,877 PointsThank you for your help, however...
this only works once. Let me explain. I am noticing that when i type in a name that IS there, it will indeed pop up. If i type in a name that is NOT there, it pops up with the error. However, where i see a flaw is when i type in a name that exists, say Dave, then type Ed, the error message never pops up.
For example, try typing in Dave, then Ed. You'll see what I mean.
Jennifer Nordell
Treehouse Teacherlevikordus You're absolutely correct! I completely forgot to set the studentFound back to false. So it just wasn't doing anything besides asking for a new search. Take a look at the line I added and give it a go
levikordus
4,877 PointsThat's the ticket! Thank you so very much! :D
I am dreading trying to figure out the duplicate student problem now :|
Jennifer Nordell
Treehouse Teacherlevikordus Well here's a thought on that. Right now we have tempIndex that's holding an index every time we find a student. And that for loop runs all the way to the end even if it finds the student in the first hit. However, there's nothing to say that tempIndex couldn't hold an array of numbers (ie an array of temporary indexes). What I mean here is we make tempIndex an array of indexes and just append the indexes when we get a hit. Just a little hint for the upcoming duplicate challenge
Jennifer Nordell
Treehouse TeacherOk, I'm not going to show you how to fix this, because I suspect you can do that yourself. What I'm going to do here is show you the logic flaw. If you'll notice, if you type in Luke, it does actually print out a report. But it doesn't do so for anyone else. Here's the code that's causing it.
search = prompt("Search for a student record: type [Levi], type quit to exit, or press cancel.");
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);
print(message);
} else if (student.name.toLowerCase() !== search.toLowerCase()){
alert("This user does not exist!")
break;
}
}
}
The for loop you have in there starts at 0. The very next line you set the student equal to the first student. This student is Luke. Now if you typed in Luke it will print out Luke's record. Then it will increment i to 1. Now the student is Levi. But you haven't yet prompted for another name. So it's checking Luke against Levi which fails. This results in what looks like odd behavior as it prints Luke's records to the document but then says that the student wasn't found.
Any other search will print out the user does not exist and exit the for loop.
edited for formatting Hope this helps!
levikordus
4,877 PointsOK, now i'm one step closer. I can get everyone's information to pop up, but it still says "Does not exist" even though they do.
** Edit 1 ***
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherThis is probably going to be easiest to troubleshoot if we can get a snapshot of your workspace. Just follow that link for instructions!