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 trialandreimocanu2
Courses Plus Student 4,510 PointsMy solution for multiple name and student not found
What do you think?
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; } for (var i = 0; i < students.length; i += 1) { student = students[i]; if ( search.toLowerCase() === student.name.toLowerCase() ) { message += getStudentReport(student); } } if ( message == '' ) { message = '<h2>The student named ' + search + ' is not in the records.</h2>'; } print(message); }
print(message);
2 Answers
Steven Parker
231,275 PointsPretty good!
But what if you searched for two students, and the second one you asked for was not found?
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 PointsSteven Parker , I see you are quite active on the forums so 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:
- create an array of objects.
- prints out the students names etc with name being an H2 and the rest of the keys being a paragraph
- Search for a specific student name and print out the information
- (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);
Steven Parker
231,275 PointsCreating a fresh question was the right way to go. See the answer I posted to the new question.
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 PointsDoron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points