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 trial

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

Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Need someone to help review my code, and provide some guidance for the last step.

perhaps you can have a look at this and help me out. quick warning I did this on my own without using the specific workspace so my names for arrays etc are different to the video's names. I copied my data straight out of atom and it was just linked to a generic index.html file. we had to:

  1. create an array of objects.
  2. prints out the students names etc with name being an H2 and the rest of the keys being a paragraph
  3. Search for a specific student name and print out the information
  4. (Bonus) if there are duplicate names, find a way to print them all out.

the issue I am having is I am unsure how to push an entire object into the array the first bit of commented out code gives me the input I wanted, however then trying step 4. I cant seem to push the entire objects data ( all 4 categories inside ) into a single array location, instead it splits each piece into a seperate location. So pushing 1 object gives me array[0] name ,array[1] track array[2] achievements array[3] points where I am trying to get array[0] =name : xyz track : xyz achievements : xyz points : xyz

I am going to post this into the questions section too

var studentClass1= [
  {
    name: "John",
    track: "Javascript",
    achievements:75,
    points:51351
  },
  {
    name: "Jacob",
    track: "Python",
    achievements:12,
    points:2322
  },
  {
    name: "John",
    track: "Ruby",
    achievements:2,
    points:100
  },
  {
    name: "Matty",
    track: "Objective-C",
    achievements:9,
    points:900
  },
  {
    name: "Claire",
    track: "Java",
    achievements:55,
    points:34215
  }
];
var studentResults= [];
// this is for EACH key in the object it will run the code.
// //for(var i=0;i<studentClass1.length;i+=1){       // this iterates through the array
//   for(var key in studentClass1[i]){             // this iterates through object keys in the current [i] value in the array
//     if(key == "name"){
//         document.write("<h2>"+key+': ' +studentClass1[i].name +"</h2>");
//     }else if(key!== " name "){
//       document.write("<p>"+key+': ' + studentClass1[i][key]+ "</p>");
//     }
//   }
// };

// this code pushes the object into the array before printing( if there are duplicate names it will print both this way otherwise the if statement only prints the last name that matches from the loop iteration.)
while(true) {
  var answer = prompt("Which student record are you looking for? type 'exit' to exit loop. ");
if(answer === "exit" || answer === null){
  break;
} else if(answer !== "exit"){
  for(var i=0;i<studentClass1.length;i+=1){
    for(var key in studentClass1[i])
    if(answer.toLowerCase() === studentClass1[i]['name'].toLowerCase()){
      studentResults.push("<li>" +key + ":  " + studentClass1[i][key]+"</li>");
    }
   }
  }
}
document.write(studentResults);

1 Answer

Steven Parker
Steven Parker
229,670 Points

You were close, you just need to accumulate the properties into a single string before you push it:

    for (var i = 0; i < studentClass1.length; i += 1) {
      if (answer.toLowerCase() === studentClass1[i].name.toLowerCase()) {
        var rec = "<ul>";
        for (var key in studentClass1[i])
          rec += "<li>" + key + ":  " + studentClass1[i][key] + "</li>";
        rec += "</ul>";
        studentResults.push(rec);
      }
    }

The list items should also be enclosed in a list element, as shown here.