Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Nikola Jankovic
Nikola Jankovic
10,793 Points

I can not understand why this work this way. I runs if and else if every time. Can someone help?

var message= ''; var student; var question;

function print(message){ document.write(message); }

function getReport (student) { var report= '<h2> Student name is '+student.name+' </h2>'; report+= '<p> Student track is '+student.track+' </p>'; report+='<p> Student achivements are ' +student.achievements+ ' </p>'; report+= '<p> Student points are '+student.points+' </p>'; return report; }

while (true) { question= prompt('Who are you looking for?'); student=students[i] if (question=== null || question.toLowerCase()=== 'quit'){ break; }

    for (var i=0; i<students.length; i+=1) {
        student=students[i];
        if (question.toLowerCase()=== student.name.toLowerCase()) {
            print(getReport(student));
        }else if (question !== student.name) {
            alert('There is no one by that name!');
            break;
        }

    }

}

2 Answers

Steven Parker
Steven Parker
215,954 Points

The loop performs the test for every student in the list.

So if you had a list of 4 students, and typed in a name that matched one of them, you would get 1 report and three alerts.

You probably want to restructure your loop so that the alert is only done after the loop completes, and then only if nothing was found and reported.

Nikola Jankovic
Nikola Jankovic
10,793 Points

Thank you for your answer. I resolved this problems with this code:

var message= '';
var student;
var question;
var answer= false;
function print(message){
        document.write(message);
}

function getReport (student) {
        var report= '<h2> Student name is '+student.name+' </h2>';
        report+= '<p> Student track is '+student.track+' </p>';
        report+='<p> Student achivements are ' +student.achievements+ ' </p>';
        report+= '<p> Student points are '+student.points+' </p>';
        return report;
}

while  (true) {
    question= prompt('Who are you looking for?');
    student=students[i]
    if (question=== null || question.toLowerCase()=== 'quit'){
        break;
    }

    for (var i=0; i<students.length; i+=1) {
        student=students[i];
        if (student.name.toLowerCase()=== question.toLowerCase() ) {
            answer= true;
             print(getReport(student));

        }   

    }


    if (! answer){
        alert('This name is not in the records!');
    }
}