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 trialOana Giurgea
Courses Plus Student 14,283 PointsI cannot resolve the last challenge: "The Student Record Search Challenge Solution"
When 2 propertie's value are the same in objects I cannot print both of them. See my code:
var students = [ { name: 'Dave', track: 'Front End Development', achievements: 158, points: 14730 }, { name: 'Jody', track: 'IOS Development with Swift', achievements: 175, points: 16375 }, { name: 'Jody', track: 'PHP Development', achievements: 55, points: 2025 }, { name: 'John', track: 'Learn WordPress', achievements: 55, points: 92025 }, { name: 'Trish', track: 'Rails Development', achievements: 5, points: 350 } ];
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('Search student records: type a name [Jody] (or type "quit" to end)'); if (search === null || search.toLowerCase() === 'quit') { break; } else { print("Sorry, we don't find " + search + " in the list."); }
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (student.name === search) {
message = getStudentReport(student);
print(message);
}
}
}
5 Answers
Benjamin Barslev Nielsen
18,958 PointsWhen do you clear the content of the div? I have assumed that you cleared the content for each iteration in the while loop, such that you do not see old results, when you enter a new name. Therefore I have added the following statement after the prompt:
document.getElementById('output').innerHTML = "";
And the if-statement should go inside the while loop, which means the final code looks like this:
while (true) {
search = prompt('Search student records: type a name [Jody] (or type "quit" to end)');
document.getElementById('output').innerHTML = "";
if (search === null || search.toLowerCase() === 'quit') { break; }
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (student.name === search) {
message = getStudentReport(student);
print(message);
}
}
if (document.getElementById('output').innerHTML === "") {
print("Sorry, we don't find " + search + " in the list.");
}
}
Oana Giurgea
Courses Plus Student 14,283 PointsThank's Benjamin! The first solution works very well. The second doesn't work. Logically should work. See my code below, please!
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 += 1) {
student = students[i];
if (student.name === search) {
message = getStudentReport(student);
print(message);
}
}
}
if (document.getElementById('output').innerHTML === "") { print("Sorry, we don't find " + search + " in the list."); }
Thank's again!
Oana Giurgea
Courses Plus Student 14,283 PointsHi Benjamin! Thank's a lot! The first solution works very well. The second doesn't work. I don't know why. But, it should work logically. See my code below, please! 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 += 1) {
student = students[i];
if (student.name === search) {
message = getStudentReport(student);
print(message);
}
}
}
if (document.getElementById('output').innerHTML === "") { print("Sorry, we don't find " + search + " in the list."); }
Thank's again!
Sonja Tomcic
6,590 PointsBenjamin Barslev Nielsen I tried your solution and it works finally. Thanks! Here is my code, so I would be grateful if you could help me resolve challenge with two same names on the list. I'm not sure how to do that.
var students=[
{
name:"Ann",
track: "Front End Development",
achievements: 50,
points: 3568
},
{
name:"Nicolas",
track: "iOS",
achievements: 45,
points: 2332
},
{
name:"Dave",
track: "Web Design",
achievements: 89,
points: 10657
},
{
name:"Mina",
track: "Java Web Development",
achievements: 34,
points: 1646
},
{
name:"Steve",
track: "Rails Web Development",
achievements: 139,
points: 12657
}
];
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>Achievements: ' + student.achievements + '</p>';
report+='<p>Points: ' + student.points + '</p>';
return report;
}
var message='';
var student;
var search;
while(true){
search=prompt("Search student records: Type a name or type 'quit' to end");
if (search===null || search.toLowerCase()==='quit'){
break;
}
var studentExist=false;
for(var i=0; i<students.length; i+=1){
student=students[i];
if(student.name===search){
message=getStudentReport(student);
studentExist=true;
print(message);
}
}
if(!studentExist){
message="Sorry, we don't find " + search + " in the list";
print(message);
}
}
Benjamin Barslev Nielsen
18,958 PointsThe problem is that your print method resets the message every time, i.e., student number two would overwrite student number one, if they had the same name. This can be solved by letting the print method add the message to the divs content instead of replacing it. This means that in the print method:
outputDiv.innerHTML = message;
should be replaced by
outputDiv.innerHTML += message;
Now you can show multiple students, so the only thing that's missing, is to clear the message every time the user provides a new student name, such that old student information is not shown. In particularly this means that you could clear the div right after the prompt, i.e. insert
outputDiv.innerHTML = "";
as the second line inside your while loop. I Hope this helps.
Benjamin Barslev Nielsen
18,958 PointsThe problem is in your print-function.
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
Every time print is called you assign the new message to innerHTML of the div, i.e., you overwrite what is already in the div. This means that if two students have the same name, then the second students information overwrites the information about the first student. This could have been solved by using += instead of =
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML += message;
}
You then also need to clear the innerHTML of the output div for each iteration in the while loop.
Oana Giurgea
Courses Plus Student 14,283 PointsThank's Benjamin! I've changed this: outputDiv.innerHTML += message; But, I cannot print the message: print("Sorry, we don't find " + search + " in the list."); Can you help me with this? Regards, Catalin
Benjamin Barslev Nielsen
18,958 PointsOh, I see the problem, I didn't realize that you printed that you couldn't find the student before you actually check if that student exist. That seems conceptually wrong, and could also behave wrong if for instance the users was stored at a server, such that there could be a delay before you had the response. In that case the user would see "Sorry, we don't find ...", until the response is received from the server. Therefore it might be better to follow the more logical approach, first check if we have a student of that name, and afterwards output that we don't have any students of that name, if that's the case. This can be done by introducing a boolean like shown here:
var studentExist = false;
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (student.name === search) {
message = getStudentReport(student);
studentExist = true;
print(message);
}
}
if(!studentExist) {
print("Sorry, we don't find " + search + " in the list.");
}
The same idea can be implemented without the boolean, by using the fact that the div element is empty after the loop, only if there were no students.
if(document.getElementById('output').innerHTML === "") {
print("Sorry, we don't find " + search + " in the list.");
}
Using the boolean makes the code easier to read, whereas the last solution makes the code shorter. I would suggest going with the boolean solution, since that solution is much easier for others to read and for yourself if you should look back at the code some time in the future.
Oana Giurgea
Courses Plus Student 14,283 PointsOana Giurgea
Courses Plus Student 14,283 PointsIt works very well! Thank;s Benjamin!