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

Why cant this toLowerCase(); work correctly in the script?

Hi, basically when I tried to do the challenge I used a code snippet from a previous video.

To do the codesnippet in some of the previous videos you could do like this:

while ( true ) {
search = prompt("Search student records: type a name [Caspar] (or type 'quit' to end)");
search = search.toLowerCase();
if ( search === 'quit' ) {
    break;
  }
  for ( var i = 0; i < students.length; i += 1 ) {
  student = students[i];
  if ( student.name === search ) {
    message = getStudentReport( student );
    print(message);
  }

}
}

But this does not work here, you need to have the .toLowerCase in the if statement otherwise the code will not work properly.

Can someone explain this for me? =)

If you write the if statement like this it work perfect :

if ( search.toLowerCase() === 'quit' )

But it should be the same as above?

2 Answers

Hey Caspar,

Your toLowerCase() method can work in the script perfectly. It does not have to be in the if-statement as Dave puts it. But, the important difference between your way and Dave's way is that Dave's way preserves the formatting of the original answer. Yours changes the formatting to lower case permanently. For example, if a user enters: "Caspar" for a name, Dave's way still has search equal to "Caspar". Your way converts "Caspar" to "caspar" permanently because you overwrote the value of search with the new lower case value. Does that make sense?

So, if you don't mind that the formatting is changed permanently, you can continue to use it that way.

That is just one way of programming it. In fact, I'll show you a third way that even saves you a line of code for your way ;)

while ( true ) {
//Added the toLowerCase() method to the end of the prompt
//This chains the command so that when prompt returns a value, it is 
//converted to a lower case version, the same as your way :)
search = prompt("Search student records: type a name [Caspar] (or type 'quit' to end)").toLowerCase();
if ( search === 'quit' ) {
    break;
  }
  for ( var i = 0; i < students.length; i += 1 ) {
  student = students[i];
  if ( student.name === search ) {
    message = getStudentReport( student );
    print(message);
  }
}
}

Thank you Marcus!

Yeah I understand now why my code snippet didn't work properly. Changed the original name "Caspar" to "caspar" in the other JS file and it work perfect know when I use the search function.

Your code works properly regardless of whether you do it your way or Dave's way. That was the point of my answer to tell you that your code does work as expected...just in a different way.