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 trialSonja Tomcic
6,590 PointsI don't know how to get this challenge done
I tried several times but it is wrong every time. Here is my code, so I will be grateful if someone helps me. I tried with else statement, but it seems that is not sollution. Please help me.
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;
}
for(var i=0; i<students.length; i+=1){
student=students[i];
if(student.name===search){
message=getStudentReport(student);
print(message);
}
}
}
Oana Giurgea
Courses Plus Student 14,283 PointsHi Sonja,
The solution is here:
var message = ''; var student; var search;
function print(message) { var outputDiv = document.getElementById('output'); outputDiv.innerHTML += message; }
function getStudentReport(student) { var report = ''; 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)'); 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.");
}
}
Have a nice day!
5 Answers
Erik Nuber
20,629 PointsYou could create an empty array and place the students that match the name into that array. Than when it goes thru all the students, you can print out a list of the found students. This would also allow you to find out if no student exists. For that you can use...
if (foundStudents.length > 0) {
print list of found students
} else {
message that no student matched the search
}
It would take more than just this but, at least hopefully it gives you an idea for how to figure it out.
In this case rather than try to store the whole student, you could also store just the index numbers and then use those to access the original students list.
Sonja Tomcic
6,590 PointsThank you Erik
Sonja Tomcic
6,590 PointsOana Giurgea Thanks, I found solution right at your question you posted a couple of days ago. Thanks again!
Sonja Tomcic
6,590 PointsJennifer Nordell Hi! Do you maybe know how to do challenge with same names? It's really hard :/
Jennifer Nordell
Treehouse TeacherHi there Sonja Tomcic. There are a couple of ways to do this, but I'd advise that you see Steven Parker 's solution here:
Hope this helps!
Sonja Tomcic
6,590 PointsJennifer Nordell Thanks a lot!
Erik Nuber
20,629 Pointshttps://teamtreehouse.com/community/finally-got-extra-credit-code-done-here-it-is
I explained everything step by step in the link. Here I used the array method, finding all students and pushing them into the array rather than building up a message which is obviously fine as well.
Sonja Tomcic
6,590 PointsSonja Tomcic
6,590 PointsIt works when it's done like this, but I want to improve it with message if that student doesn't exist and with search if there are two students with same name. I'm trying to solve this for two days...