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 trialSal Mike
71 PointsCan any one explain the below code: html += studentRecords(students[studentNames.indexOf(search)]);
Can any one explain the below code: html += studentRecords(students[studentNames.indexOf(search)]) ...????
The full code is below:
function print(message){ var div = document.getElementById("write"); div.innerHTML = message; }
function studentRecords(student){ html = "<h2> Student: " + student.name + "</h2>"; html += "<p> Track: " + student.track + "</p>"; html += "<p> Achievements: " + student.achievements + "</p>"; html += "<p> Points: " + student.points + "</p>"; return html; }
var students = [ { name: "Tom", track: "iOS Development", achievements: 43, points: 73839 }, { name: "Jerry", track: "Web Design", achievements: 23, points: 89498 }, { name: "Sam", track: "Front End Development", achievements: 86, points: 23187 }, { name: "Kim", track: "Back End Development", achievements: 24, points: 90594 }, { name: "Jane", track: "Java Development", achievements: 73, points: 87328 } ];
var search; var html = ""; var studentNames = [];
while(true){ search = prompt("Enter a Students Name: [or 'x' to quit]"); if (search === null || search.toLowerCase() === "x") { break; } for (var i = 0; i < students.length; i++) { studentNames.push(students[i].name.toLowerCase()); } if (studentNames.indexOf(search.toLowerCase()) > -1) { html += studentRecords(students[studentNames.indexOf(search)]); print(html); } else { print("<h2>" + search + " not found</h2>"); } break; }
1 Answer
Steven Parker
231,275 PointsDo you mean explain why it's not working? Unless you type a name in all lower case, it won't be found because it tries to find the name in an array that has already been converted.
If you mean explain what it does:
-
indexOf()
will give you the index number wheresearch
is found in thestudentNames
array -
students[...]
will return the object at that index number of thestudents
array -
studentRecords(...)
is a function that converts the properties of thatstudent
object into a string -
html += ...
and that string is then added on the end of the string namedhtml