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

Dmitri Antonov
PLUS
Dmitri Antonov
Courses Plus Student 6,010 Points

Pls kindly advise...

Hi everyone.

If objects property name has both firstname and second name in it, how to print it on a webpage using only first name as an input???... For example i type "David" as an input and get details of all "Davids" which i have in array of objects?

Pls check below code, thx.

var count = 0; var message =""; //array of objects holding list of students

var student = [

{ name: "David Johnson", age: "23", course: "Java", level: "Guru" },

{ name: "David Johnson", age: "24", course: "HTML", level: "Professional" },

{ name: "Brigita Popovic", age: "28", course: "PHP", level: "intermediate" },

{ name: "John Lennon", age: "23", course: "HTML", level: "Rookie" }

];

function print(report) { var outputDiv = document.getElementById("output"); outputDiv.innerHTML = report; }

while(true) {

var input = prompt ("Please insert name of student..."); input = input.toLowerCase(); if (input === "quit" || input === null) { break; }

for (var key in student) { if (input === student[key].name.toLowerCase()) { message += ("<h2>Student: " + student[key].name + "</h2>"); message += ("<p>Age: " + student[key].age + "</p>"); message += ("<p>Course: " +student[key].course + "</p>"); message += ("<p>Level: " + student[key].level + "</p>"); count += 1; print(message);
} } if(count === 0){ alert("No students with such name");

} }

2 Answers

Steven Parker
Steven Parker
231,248 Points

You could check just the beginning of the name.

You could take a slice of the name and compare that, but there's also a handy string method that compares just the beginning called startsWith:

if (student[key].name.toLowerCase().startsWith(input + ' '))  //...

Note that a space is added to the input so that it will only match a complete first name.

Anthony McCormick
Anthony McCormick
6,774 Points

Trying this out for myself but doesn't appear to work?

Steven Parker
Steven Parker
231,248 Points

Did you start with Dimitri's code and replace the first if statement in the loop? That's what I did and it seemed to work well.

Anthony McCormick
Anthony McCormick
6,774 Points

Is it updated to the original if statement or is it a brand new one? Here is a copy:-

while (true) {

    var input = prompt("Please insert name of student...");
    input = input.toLowerCase();
    if (input === "quit" || input === null) {
        break;
    }

    for (var key in student) {
        if (input === student[key].name.toLowerCase().startsWith(input + ' ')) {
            message += ("<h2>Student: " + student[key].name + "</h2>");
            message += ("<p>Age: " + student[key].age + "</p>");
            message += ("<p>Course: " + student[key].course + "</p>");
            message += ("<p>Level: " + student[key].level + "</p>");
            print(message);
        }
    }
}
Steven Parker
Steven Parker
231,248 Points

This is not the if statement I gave above. Look closely, you added something to it that makes it not work.

Anthony McCormick
Anthony McCormick
6,774 Points

I am using Dimtri's code here.

So are you saying that instead of:-

 if (input === student[key].name.toLowerCase().startsWith(input + ' ')) {

It needs to simply be:-

if (student[key].name.toLowerCase().startsWith(input + ' '))
Steven Parker
Steven Parker
231,248 Points

Yes, that's my suggested test for just the first word. But you'll still need the open brace.

Dmitri Antonov
PLUS
Dmitri Antonov
Courses Plus Student 6,010 Points

Hi Steven, thx a lot for info.

One more question:

Here is the code:

var count = 0; var message =""; //array of objects holding list of students

var student = [

{ name: "David Johnson", age: "23", course: "Java", level: "Guru" },

{ name: "David Johnson", age: "24", course: "HTML", level: "Professional" },

{ name: "Brigita Popovic", age: "28", course: "PHP", level: "intermediate" },

{ name: "John Lennon", age: "23", course: "HTML", level: "Rookie" }

];

function print(report) { var outputDiv = document.getElementById("output"); outputDiv.innerHTML = report; }

function getFinalOutput (student){ var report = ("<h2>Student: " + student[key].name + "</h2>"); report += ("<p>Age: " + student[key].age + "</p>"); report += ("<p>Course: " +student[key].course + "</p>"); report += ("<p>Level: " + student[key].level + "</p>"); return(report); }

while(true) {

var input = prompt ("Please insert name of student..."); input = input.toLowerCase(); if (input === "quit" || input === null) { break; }

for (var key in student) { if (input === student[key].name.toLowerCase()) { count += 1; message += getFinalOutput(student); print(message); } } if(count === 0){ alert("No students with such name");

} }

I am trying to understand what is the point of parameter in getFinalOutput() function. In the code example it is function getFinalOutput(student) and code is working fine. So i'm quite confused to what does it refer to? If i change it to any other (for example - person) and in for in loop conditional statement will change code to message += getFinalOutput(person) i will get empty page as otput and loop ends, but in case i leave it just empty - function getFinalOutput() and in for in loop conditional statement change code to message += getFinalOutput(), i will get the result.

Pls kindly advise.

thx

Steven Parker
Steven Parker
231,248 Points

I don't get a result if I remove the parameter, so I'm not sure what you are seeing. But I think you may have an error in how the function works. As it is, it relies on access to the global variable key, which is not a good idea. It would be better if the function worked for just one selected student, like this:

function getFinalOutput(student) {
  var report = ("<h2>Student: " + student.name + "</h2>");
  report += ("<p>Age: " + student.age + "</p>");
  report += ("<p>Course: " + student.course + "</p>");
  report += ("<p>Level: " + student.level + "</p>");
  return (report);
}

And you would call it like this:

      message += getFinalOutput(student[key]);