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

Ivan Kusakovic
Ivan Kusakovic
12,197 Points

Students for loops?

This is the closest I can understand, but this doesn't work.

var message= "";
var student;
var student2;
var students= [
  {
    name: "Ivan",
    track: "Front-End Development",
    achievements: "13 badges",
    points: 5163
  },
  {
    name: "Sara",
    track: "Back-End Development",
    achievements: "12 badges",
    points: 500
  },
  {
    name: "Mark",
    track: "Design",
    achievements: "11 badges",
    points: 300
  },
  {
    name: "John",
    track: "Business",
    achievements: "11 badges",
    points: 2001
  },
  {
    name: "Rob",
    track: "iOS",
    achievements: "10 badges",
    points: 100
  },
  {
    name: "Rob",
    track: "Markic",
    achievements: "113123 badges",
    points: 100133
  }
];



function print(message){
  var outputDiv = document.getElementById("output");
  outputDiv.innerHTML = message;
}

function getStudentReport(student){
  var report = "<h2>Student: " + student.name + "</h2>";
  report += "<h2>Track: " + student.track + "</h2>";
  report += "<h2>Points: " + student.points + "</h2>";
  report += "<h2>Achievements: " + student.achievements + "</h2>";
  return report;
}

while(true){
  var pr1 = prompt("Enter name or quit to close:");
  if (pr1 ===null || pr1 === "quit"){
    break;
  }
  for (var i = 0; i<students.length; i+=1){
    for (var j=0; j<students.length; j+=1){
    if (students[i].name === students[j].name ){
      message= getStudentReport(students[i]) + getStudentReport(students[j]);
      print(message);
    }else if (students[i].name ===pr1) {
      student= students[0];
      message=getStudentReport(student);
      print(message);
    }
  }

}
}

Can anyone explain me am I on good way?

Cheers

3 Answers

Jesus Mendoza
Jesus Mendoza
23,275 Points

Hey Ivan, I came with this solution. It is basically the same code as the video except for the print, i move it outside the if, so every time it loops through the for loop it will print whatever it is in the message variable.

var message = '';
var student;
var studentName;

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) {
  studentName = prompt("Whats the name of the student?");  
  if (studentName.toLowerCase() === 'quit' || studentName === null) {
    break;
  }

  for (var i = 0; i < students.length; i += 1) {
    student = students[i];   
    if (studentName.toLowerCase() === student.name.toLowerCase()) {
      message += getStudentReport(student); 
    }

    print(message);
  }
}
Ivan Kusakovic
Ivan Kusakovic
12,197 Points

Fantastic! I thought that is something like that. Thanks :)

Ivan Kusakovic
Ivan Kusakovic
12,197 Points

Hey, but I wanted to check if there is two identical names :) Jesus :)