Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);
                } 
            }
        }
}