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 trialSamuel Trejo
4,563 PointsMy Solution to Final Challenge
Here is my solution, using what was taught up until now in this course. If name is not found it will print to document not found, and if there are duplicate names it will print both names and info. Hope someone finds this helpful was frustrating but fun to figure it out. Let me know if you have any feedback, or questions about it.
var students = [
{name: "Davy", track: "iOS", achievements: 5, points: 525},
{name: "Ito", track: "Web Design", achievements: 6, points: 25346},
{name: "Sami", track: "JavaScript", achievements: 7, points: 7467},
{name: "Dany", track: "Java", achievements: 9, points: 98769},
{name: "Davy", track: "iOS", achievements: 8, points: 13434}
];
var msg = "";
var student;
var search = "";
var name;
var notFound;
var counter = 0;
function print(msg){
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = msg;
}
function getStudentData(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
}
while(search.toLowerCase() !== "done"){
search = prompt("Type a name to search, to exit search type \"done\".");
for(i = 0; i < students.length; i++){
student = students[i];
counter++;
if(search.toLowerCase() === student.name.toLowerCase()){
msg = getStudentData(student);
notFound = false
print(msg);
break
}else if(search === "done"){
break
}else{
notFound = true
}
}
for(j = counter; j < students.length; j++){
student = students[j];
if(search.toLowerCase() === student.name.toLowerCase()){
msg += getStudentData(student);
print(msg);
}
}
if(notFound === true && msg === ""){
msg = "Sorry, no student found with that name."
print(msg)
search = "done"
}
print(msg)
}
1 Answer
Johan Uribe
2,677 PointsJust looking through your code, it only solves the problem when two students have the same name. If more than two students have the same name, then your code no longer works. I think. I am learning this stuff right along with you. I am pretty sure that the solution involves finding the index number of the first student with the name, saving that index number as i, and then rerunning another search through that array for any other students with that name starting at index value i+1 by using the array.indexOf(search, i+1) syntax in a loop that will keep iterating over the array until indexOf returns a -1.
Samuel Trejo
4,563 PointsSamuel Trejo
4,563 PointsHey Johan, it should work with more than 2 students. Try it here: https://codepen.io/To0ns/pen/BGMRzB?editors=1011
Curious to know why it seems like it wouldn't work or if I'm missing something let me know. Let me know if you need me to explain anything.
Thanks for the reply!