Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Sonja Tomcic
Sonja Tomcic
6,590 Points

I 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);


    }

  }

}
Sonja Tomcic
Sonja Tomcic
6,590 Points

It 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...

Oana Giurgea
Oana Giurgea
Courses Plus Student 14,283 Points

Hi 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
Erik Nuber
20,629 Points

You 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
Sonja Tomcic
6,590 Points

Oana Giurgea Thanks, I found solution right at your question you posted a couple of days ago. Thanks again!

Sonja Tomcic
Sonja Tomcic
6,590 Points

Jennifer Nordell Hi! Do you maybe know how to do challenge with same names? It's really hard :/

Erik Nuber
Erik Nuber
20,629 Points

https://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.