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

Jennifer Hughes
Jennifer Hughes
11,421 Points

Searching for a person's name

Hi! I am doing the final two parts of the challenge, and I cannot seem to resolve this one.

If someone searches for a person who does not exist, I want to prompt the person to search again, and store that input into the "search" variable. It isn't working, and I am not sure the correct way to do this.

Any advice?

Thanks!

var message = '';
var student;
var search;

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

function getStudentReport(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(true) {
    search = prompt("Search for a person's name or type 'quit' to end");
    if (search === null || search.toLowerCase() === 'quit') {
        break;
    }   
    for(var i = 0; i < students.length; i++) {
        student = students[i];
    //store one student in the student variable
         if (student.name.toLowerCase() === search.toLowerCase()) {
            message += getStudentReport(student);
            print(message);
    }
        else {
            search = prompt("Sorry, that name was not found. Search for a person's name or type 'quit' to end");
        }
    }
}

4 Answers

Blake Lieberman
Blake Lieberman
23,772 Points

Students is undefined. There is a starting point.

Jennifer Hughes
Jennifer Hughes
11,421 Points

Blake, thanks for taking a peak; however, students is defined in another js file, which contains all of the relevant data.

students = [
    {name: "jenn", track: 'Web Development', achievements: '5', points: '6000'},
    {name: "andrew", track: 'Front End', achievements: '7', points: '6800'},
    {name: "andrea", track: 'Ios', achievements: '51', points: '26000'},
    {name: "david", track: 'Ruby', achievements: '15', points: '16000'},
    {name: "michael", track: 'PHP', achievements: '3', points: '3000'}
];

So, back to my originally question: If someone searches for a student that does not exist, how can I alert the person of this and allow him to do another search? That's what I am after. Thanks!

Blake Lieberman
Blake Lieberman
23,772 Points

The break keyword ends code running inside the while loop. That is where the problem begins.

Hi Jennifer, I'm just finishing off the same exercise. I think your problem is that the new value that you assign to 'search' isn't ever entering the loop. That value will be over-written once the while loop starts again when the original prompt command executes.

The only way to update the prompt text is to store that in it's own variable - so var promptText = "Search student records: Type a name [Jimmy] (or type 'quit' to end)"; search = prompt(promptText);

and then if a value is NOT found, update that variable with promptText = "Sorry that value was not found. " + promptText;

you would need to reset the variable after any subsequent successful search.

Cheers Del