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

emanelbaradei
emanelbaradei
11,395 Points

Please help.. (part 1 of the additional tasks) I hope someone could explain in detail why my loop is working wrong..

Ive been trying for more than a full day.. but i just dont understand why is it printing correctly the existing names, but not printing the message of not existing names, although it gets printed on first try of typing wrong name! ... i think im missing something or maybe im not understanding the loops as well as i should... i really need someone to just help me understand this in "extra" detail.. Thanks in advance for reading! Here is my code:

//------ array of objects
var persons = [
  {name: "Merna", job: "none", age: 18},
  {name: "Hala", job: "fulltime", age: 27},
  {name: "Lolo", job: "parttime", age: 35},
  {name: "Baky", job: "none", age: 16},
  {name: "Yara", job: "studies", age: 20}
];
//------ variables:
var person;
var search;
var found = false;


//-------functions:

//function to print in html
function print(message) {
  var div = document.getElementById("output");
  output.innerHTML = message;
}
//function to print the person we are searching for:
function printList(person) {
  var list = "<h2> Name: " + person.name + "</h2>";
  list += "<p>Job: " + person.job + "</p>";
  list += "<p>Age: " + person.age + "</p>";
  return list;
}

//------loops
// while loop to make a question and answer
while (true) {
  search = prompt("Search any Name: (write DONE to stop) ");

  if (search.toUpperCase() === "DONE" || search === null) {
    break;
  }
// for loop to search existing students:
   for (var i = 0; i < persons.length; i += 1) {

    person = persons[i];

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

      found = true;
      list = printList(person);
      print(list);

    } else if (!found) {

//here is the problem: when i search for a wrong name, it just doesnt print this, it only prints it on first try.
       print("This Name: " + search + " is not found!");
      }
    }
}

2 Answers

Steven Parker
Steven Parker
231,007 Points

Each call to print replaces ("=") the content of the output rather than adding to it ("+="), so only the final one will be seen.

Also, the search will only match names entered in lower case. To make it case insensitive, you could convert the search term to lowercase as well:

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

You might also want to handle the output of an unfound name after the loop. Once you fix the first issue you'll notice it will appear for each name it is compared with.

emanelbaradei
emanelbaradei
11,395 Points

ok.. please be patient with me.. i do understand that the "=" overrides the name that we are searching, and that if we added (+=) it will print all names that we searched for, but my problem is not here, my problem is in this code:

 else if (!found) {
//here is the problem: when i search for a wrong name, it just doesnt print this, it only prints it on first try.
       print("This Name: " + search + " is not found!");
      }

I would like that when a user type a wrong name, he gets this message "This Name: " "the wrong name" " is not found!" even if its overriding the right name that might be printed before (but i guess it shouldnt override it as it doesnt have anything to do with the function of list), but its not working this way.. this message only is printed if the first time i try to type a "wrong" name.. but after that it just stops as if the message doesn't exist anymore

Steven Parker
Steven Parker
231,007 Points

Whenever a name is found, the variable "found" is set to "true". But it is never changed back to "false", so any bad names entered after that will not be reported. So you can see many bad names printed, but only if they are all entered before any existing name.

emanelbaradei
emanelbaradei
11,395 Points

Oh, RIGHT!! Thank you soo much! i been going crazy for this!