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

Hmmm brain hurting!

I don't know about anyone else, but I found this challenge very difficult. Knew that I had to use the while loop, but couldnt see how to search the object within the array. Anyhow I just followed along with Dave in the end and can see how hid code works, so copied it to see it in action and for some reason it doesnt want to work (to me - mine matches exactly)....any ideas peeps?

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

while (true) {
  search = prompt ('Type Students Name, or type "quit" to leave store.');
  if ( search === null || search.toLowerCase() === 'quit') {
     break;
  } 
  for (var i = 0; i < students.length; i += 1) {
     student = students[i];
     if (student.name === search) {
     message = getStudentReport(student);
       print(message);

  }
}
}```

2 Answers

That only thing i spotted was this check:

search === null

According to the Mozilla docs:

When the user clicks the OK button, text entered in the input field is returned. If the user clicks OK without entering any text, an empty string is returned.

Meaning you should change that check to either:

!search

//or

search === ''

I put together a quick example that works:

var search;
var students = [
    {id: 1, name: 'Tom'},
    {id: 2, name: 'Jane'},
    {id: 3, name: 'Milo'},
    {id: 4, name: 'Jane'},
    {id: 5, name: 'Tom'}
];



while (true) {
    search = prompt('Type Students Name, or type "quit" to leave store.');
    if (!search || search.toLowerCase() === 'quit') {
         break;
    } 
    for (var i = 0; i < students.length; i += 1) {
         student = students[i];
         if (student.name === search) {
            document.write(JSON.stringify(student));
        }
    }

}

Also, I gave this answer to another post sometime ago:

An alternative way to perform the search is via regex:

var search = function(what) {
    var pattern = new RegExp('\\b' + what + '\\b', 'g');
    return (students.map(student => student.name).join()).match(pattern);
};

var result = search('Tom');

console.log(result); //returns [ 'Tom', 'Tom' ]

Hi Thomas,

The reason for the null check is because the prompt will return null if you cancel it.

The intention is to quit the program if they cancel the prompt or type quit.

With an empty string you would still perform the search but you won't match anything.

Using search === '' would lead to a type error if they cancelled the prompt because you would be calling the .toLowerCase() method on null.

!search would work out but you're changing the behavior of the original code.

Thanks for the input Thomas, Jason can you see where I am going wrong?

Looking at the code, nothing is standing out.

Can you post a workspace snapshot? Or link to the video you're on?