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

Marcell Hawke
Marcell Hawke
7,612 Points

Can someone please help me with the extras of my code?

I've added an "else if" at the end of my code, but ever since I did that whatever name I type it will say that the student is not in class even if their name is in the list. Why is that please?

My code:

var message = " ";
var student;
var search;

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

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

while (true){
    search = prompt("Search student records: type a name[Jody] or type 'quit' to close.")
    if (search === null || search.toLowerCase() === 'quit'){
        break;
    }
    for (var i = 0; i < students.length; i++){
        student = students[i];
        if (student.name.toLowerCase() === search.toLowerCase()) {
            message = getStudentReport(student)
            print(message)
        } else if  (student.name.toLowerCase() !== search.toLowerCase()) {
            message = "<p> <b>" + search + "</b> is not in the class</p>"
            print(message);
        }
    }
}
Joseph Sworyk
Joseph Sworyk
7,879 Points

Change' !==' to '!='

ie only need one equals sign when checking for inequality.

This should do the trick.

Marcell Hawke
Marcell Hawke
7,612 Points

I'm still get the same error Joseph! :( Every single name says that the person is not in class!

PS) How do I quote someone?

Joseph Sworyk
Joseph Sworyk
7,879 Points

var student;

Should this variable not be an array of student names in the class? Based on this empty variable, there are no students in your class so yeah it would make sense that it says they are not in the class when you search. ?

1 Answer

Marcell Hawke
Marcell Hawke
7,612 Points

It seems that I solved it!

var message = " ";
var student;
var search;
var find = false;

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

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

while (true){
    search = prompt("Search student records: type a name[Jody] or type 'quit' to close.")
    find = false;
    if (search === null || search.toLowerCase() === 'quit'){
        break;
    }
    for (var i = 0; i < students.length; i++){
        student = students[i];
        if (student.name.toLowerCase() === search.toLowerCase()) {
            message = getStudentReport(student)
            find = true;
            print(message)
        }
    }
    if (! find) {
        print("STUDENT NOT HERE")
    }

}