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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Moving the code from the for loop into the function, changes how the program works. Did I miss something?

while the block of code is still inside the for loop, I can enter several names and when i exit it prints out all the names and values as I expect. when I move that code into the function call it... only the last name prints to the page after I hit quit.

function print(output){
    document.getElementById("display").innerHTML = output;
}

var students = [
    {
        name: "Estudientes",
        subject: "Espaniol",
        pass: true,
        effort: "Good"
    },
    {
        name: "Moi",
        subject: "Esotericism",
        pass: "subjective",
        effort: "Limitless"
    },
    {
        name: "Populous",
        subject: "Mind Control",
        pass: "Why resist?",
        effort: "You tell me"
    },
    {
        name: "Minion",
        subject: "I just follow",
        pass: "Please like me",
        effort: "I'll do whatever you say"
    }

];

function studentReport(student){
    var report = "<h3>Student: " + student.name + "</h3>"
    + "<li>Subject: " + student.subject + "</li>"
    + "<li>Pass: " + student.pass + "</li>"
    + "<li>Effort: " + student.effort + "</li>"
    return report;
}

var message = "", student, i, search;

while(true){
    search = prompt("Enter a namme to search or 'quit' to exit");
    if(search === null || search.toLowerCase() === "quit" || search === ""){
        break;
    }
    for(i = 0; i < students.length; i  += 1){
        student = students[i];
    if(student.name.toLowerCase() === search.toLowerCase()){
            // message = studentReport(student);
            // move to studentReport
            message += "<h3>Student: " + student.name + "</h3>"
            + "<li>Subject: " + student.subject + "</li>"
            + "<li>Pass: " + student.pass + "</li>"
            + "<li>Effort: " + student.effort + "</li>"
        } /* if student.name === saerch */

    } /* for */

} /* while */




print(message);

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I'm guessing you did miss something. I have no idea what though as I can't see what you tried to change it to. So I reworked your code a bit to use a function to conduct the search. Take a peek :smiley:

function print(output){
    document.getElementById("display").innerHTML = output;
}

var students = [
    {
        name: "Estudientes",
        subject: "Espaniol",
        pass: true,
        effort: "Good"
    },
    {
        name: "Moi",
        subject: "Esotericism",
        pass: "subjective",
        effort: "Limitless"
    },
    {
        name: "Populous",
        subject: "Mind Control",
        pass: "Why resist?",
        effort: "You tell me"
    },
    {
        name: "Minion",
        subject: "I just follow",
        pass: "Please like me",
        effort: "I'll do whatever you say"
    }

];

function studentReport(student){
    var report = "<h3>Student: " + student.name + "</h3>"
    + "<li>Subject: " + student.subject + "</li>"
    + "<li>Pass: " + student.pass + "</li>"
    + "<li>Effort: " + student.effort + "</li>"
    return report;
}

var message = "", student, i, search;

while(true){
    search = prompt("Enter a namme to search or 'quit' to exit");
    if(search === null || search.toLowerCase() === "quit" || search === ""){
        break;
    }
    searchStudent(search);
    /*
    for(i = 0; i < students.length; i  += 1){
        student = students[i];
    if(student.name.toLowerCase() === search.toLowerCase()){
            // message = studentReport(student);
            // move to studentReport
            message += "<h3>Student: " + student.name + "</h3>"
            + "<li>Subject: " + student.subject + "</li>"
            + "<li>Pass: " + student.pass + "</li>"
            + "<li>Effort: " + student.effort + "</li>"
        } /* if student.name === saerch */

    } /* for */

/* while */

function searchStudent(search){
    for(i = 0; i < students.length; i  += 1){
        student = students[i];
        if(student.name.toLowerCase() === search.toLowerCase()){
            // message = studentReport(student);
            // move to studentReport
            message += "<h3>Student: " + student.name + "</h3>"
            + "<li>Subject: " + student.subject + "</li>"
            + "<li>Pass: " + student.pass + "</li>"
            + "<li>Effort: " + student.effort + "</li>";
        } /* if student.name === saerch */

    }
}

print(message);

Hope this helps! :sparkles:

Well that works :D I'm gonna take a while and chew on that. Thank you.

Ok, I see how you did that. Very concise. Thanks again.