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

Daniel Sokol
Daniel Sokol
14,888 Points

My Solution to the Search Extension Challenge

Here it is - any suggestions to improve would be welcome - Thanks!

var students = [
    {Name: "Dan", Track: "iOS Web Design", Achievements: 12, Points: 3456},
    {Name: "Dan", Track: "Front End", Achievements: 3, Points: 4656},
    {Name: "Frank", Track: "iOS Web Design", Achievements: 33, Points: 45555},
    {Name: "Sally", Track: "Graphic Design", Achievements: 22, Points: 4321},
    {Name: "Rich", Track: "Full Stack", Achievements: 45, Points: 44321},
    {Name: "Rich", Track: "ANything", Achievements: 45, Points: 234234}
];

function print(message, id) {
  var outputDiv = document.getElementById('id');
  id.innerHTML = message;
}


function getStudentInfo (studsFound) {
        var list = "<h2>Student:</h2>";
        list+= "<p>Name: " + (studsFound.Name) + "</p>";
        list+= "<p>Track: " + (studsFound.Track) + "</p>";
        list+= "<p>Achievements: " + (studsFound.Achievements) + "</p>";
        list+= "<p>Points: " + (studsFound.Points) + "</p><hr>";
        return list;
    }

while (true) {
    var ask = prompt("Enter Student name or 'quit' to exit");
        if (ask.toUpperCase() === "QUIT" || ask === null){
            var message = "Goodbye";
            print(message,output);
            break;

        } else { 
            var newList = [];
            message="";
            var found = false;

            for (var a=0; a<students.length; a+=1) {
            if (ask.toUpperCase() === students[a].Name.toUpperCase()){
                found = true;
                newList.push(students[a]);
            } else {message = ask + " not found.";
                   print(message,output);
                }
            }if (found = true){     
                    message = "";
                for (x=0; x<newList.length; x+=1){  
                    message+=getStudentInfo(newList[x]);
                    print(message,output);
                } 
            }
        }
}