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

My solution to the challenge

var message = '';
var student;
var search;
var registor = [];

//To print message to output div.
function print(message) {
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = message;
}

//Bring the registor into html format and stor it into report.
function getStudentReport()
{
    var report = '';

    for (var m = 0; m < registor.length; m += 1)
    {
            console.log(search);
            report += '<h2>Student: ' + registor[m].name + '</h2>';
            report += '<p>Track: ' + registor[m].track + '</p>';
            report += '<p>Points: ' + registor[m].points + '</p>';
            report += '<p>Achievements: ' + registor[m].achievements + '</p>';           
    }
    return report;    
}
// compare will check if any student have the same name.
function compare() {

    for (var i = 0; i < students.length; i += 1) {
        studentFirst = students[i];
        studentFirstName = students[i].name;
        studentLower = students[i].name.toLowerCase();

        for (var n = 0; n < students.length; n += 1) {
            student2 = students[n];
            student2Name = students[n].name;
            if ((studentFirstName === search || studentLower === search) && studentFirst.ID === student2.ID) {
                registor.push(studentFirst);
                console.log(registor);
            }
        }
    }
    return registor;
    console.log(registor)
}

while (true) {
    registor = [];
    search = prompt('Search student records: Type the name or type Quit to get out.');
    compare();
    if (search === null || search.toLowerCase() === 'quit') {
        break;
    } else {
        message = '<h2>Not a student</h2>';
        print(message);
    }
    for (var p = 0; p < students.length; p += 1) {
        student = students[p];

        if (student.name === search || student.name.toLowerCase() === search) {

            console.log(search);
            message = getStudentReport();
            print(message);
        }
    }

}

Fixed Code Presentation

7 Answers

Ok. so here's how I got it to work.

Original

while (true) {
    registor = [];
    search = prompt('Search student records: Type the name or type Quit to get out.');
    compare();
    if (search === null || search.toLowerCase() === 'quit') {
        break;
    } else {
        message = '<h2>Not a student</h2>';
        print(message);
    }
    for (var p = 0; p < students.length; p += 1) {
        student = students[p];

        if (student.name === search || student.name.toLowerCase() === search) {

            console.log(search);
            message = getStudentReport();
            print(message);
        }
    }
}

I first started by removing your OR within the conditional if statement., Then I added a .toLowerCase to your initial prompt. and Voila.

Updated JS

while (true) {
    registor = [];
    search = prompt('Search student records: Type the name or type Quit to get out.').toLowerCase();
    compare();
    if (search === null || search.toLowerCase() === 'quit') {
        break;
    } else {
        message = '<h2>Not a student</h2>';
        print(message);
    }
    for (var p = 0; p < students.length; p += 1) {
        student = students[p];

        if (student.name.toLowerCase() === search) {

            console.log(search);
            message = getStudentReport();
            print(message);
        }
    }
}

any input will be sent to lower case no matter how it is entered and therefore Jody, jOdy and jody will all pull the students records.

JsFiddle - Updated.

I hope this helps.

Thanks much. So simple.

I noticed an error since .toLowerCase() is added to the alert. "Uncaught TypeError: Cannot read property 'toLowerCase' of null" when the prompt is canceled or exited out. Not much of a problem, I think. Can't see the error unless going into inspect element. At least it works.

Thank you. How did you fix that?

Posting code on the forums

This is the best I can do so far. I am able to print as many students with the same name. Also if someone were to spell in all lower case it will still output the message. The only thing that will not work right now if someone input JOdy instead of Jody or jody for example. Any ideas on getting irregular case spelling to work?

Can you post a snapshot of your entire workspace?

Not sure how to do that?

Taking a Snapshot of your Workspaces

  1. Launch any of your workspaces.
  2. Select the snapshot menu in the upper right corner.
  3. Click "Take Snapshot"
  4. Visit the link and share it with someone.

I'm using visual studios for the time being. Trying to get a handle of it while I'm studying. Any idea on how to do the same thing?

You can post the code on an online code editor like JSFiddle.

Here is a link on how I got the challenge to work.
https://jsfiddle.net/x4mzen1a/embedded/result/

Here shows the code after testing. https://jsfiddle.net/x4mzen1a/

Thanks for the info Chyno!

My solution:

var message;
var student;
var search;
var studentNames = [];

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

function getStudentReport( student ) {
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}

for (var i = 0; i < students.length; i += 1) {
  student = students[i];
  studentNames.push(student.name.toLowerCase());
}

while (true) {
  search = prompt('Search student records: type a name [Jody] (or type "quit" to end)');
  message = '';
  if (search === null || search.toLowerCase() === 'quit') {
  break;
  }
  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if ( student.name.toLocaleLowerCase() === search ) {
      message += getStudentReport ( student );
    }
  }
  print(message);
  if (studentNames.indexOf(search.toLowerCase()) === -1) {
      print('<p>No student named "' + search + '" was found.</p>');
    }
}