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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Niyamat Almass
Niyamat Almass
8,176 Points

Please someone help me to solve the challenge

what wrong in the code. I want that if i type a student that is not in the student array the page shows that not found .But it also print not found when entered the student who is in the array.how to solve the challenge.

student_report.js
var message = '';
var student;
var search;

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>Achivement: ' + student.achievements + ' </p>';
   report += '<p>Point: ' + student.points + ' </p>';
  return report;
}

while (true) {
search = prompt( 'Search the student name' );
  if ( search === null || search.toLowerCase() === 'quit' ) {
  break;
  }
  for( var i = 0; i < students.length; i += 1 ) {
student = students[i];
    if ( student.name.toLowerCase() === search.toLowerCase() ) {
    message = getStudentReport( student );
      print(message);
    } else {
    message = 'not found';
      print(message);
    }
    }

}





print(message);

2 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Niyamat.

The problem is in your loop that loops through the students array. In particular this loop is able to find a matching name in the students array and assigns it to message via the getStudentReport function, but it does NOT stop here, it keeps looping inside the array of students and obviously, since it does not find that name anymore, we move to the else part of your code, where the message variable gets assigned to "not found".

In fact, if you substitute message = 'not found' in the else statement with message += 'not found' you get an output that will make you realize what is going on. Try it!

To quickly fix it I would add only a "break" keyword to your original code you provided when posting this question, right below, like this:

message = getStudentReport( student );
      print(message);
      break;

Also, you want to delete that print at the very bottom.

Let me know if all clear, pretty long explanation, not easy to be precise..

Vittorio

Niyamat Almass
Niyamat Almass
8,176 Points

i tried that code

student_report.js
var message = '';
var student;
var search;

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>Achivement: ' + student.achievements + ' </p>';
   report += '<p>Point: ' + student.points + ' </p>';
  return report;
}

while (true) {
search = prompt( 'Search the student name' );
  if ( search === null || search.toLowerCase() === 'quit' ) {
  break;
  }
  for( var i = 0; i < students.length; i += 1 ) {
student = students[i];
    if ( student.name.toLowerCase() === search.toLowerCase() ) {
    message = getStudentReport( student );
      print(message);
    } else {
    message += 'not found';
      print(message);
    }

    }

}





print(message);

but when i type a invalid type of student name its write to the page

html.output
not foundnot foundnot foundnot foundnot found

how to i solve this so that that code only can write not found

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hey.

That code was just to give you an idea of what is happening.

To get your desired output you should put a break keyword right where I suggested you. This would break the loop when the condition is matched.

Then remove that plus because it was only added for testing purposes and also remove the finale print(message) at the very end.

Niyamat Almass
Niyamat Almass
8,176 Points

wow i understand the fact .Thank you so much Vittorio Somaschini.

Brandon Barrette
Brandon Barrette
20,485 Points

I would do this:

var found = false;
for( var i = 0; i < students.length; i += 1 ) {
student = students[i];
    if ( student.name.toLowerCase() === search.toLowerCase() ) {
      message = getStudentReport( student );
      found = true;
      break;
    } 
}

if (found) {
  print(message);
} else {
  print("Not found");
}

So what I did is remove the else condition from the for loop. If the student is not found, I don't need to print anything YET, since it's possible the student is later in the array. So instead, I just check if the current student in the loop is the student I'm looking for, if it is, then I set the message, set the found variable to true and break out of the loop.

Now if we pass through the whole loop and don't find anyone, the variable found is still false, and then outside the loop I can check the found variable. If one was found, then I can print the message that was set in the for loop. If it was not found, I can print any message I want, like "not found" or whatever.

Hope that helps!