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

Marvin Benno
Marvin Benno
7,524 Points

Question regarding .toLowerCase and null

I looked through the video of Dave giving us his solution on the coding challange. In the video he talkes about how to fix the problem with the search.toLowerCase() giving a value of null. Can someone please explain what this means and why it happens? Also, in my code. in row 27 and row 33 I get the TypeError "Cannot read property 'toLowerCase' of null" in my chrome console. Why is that and how do I solve it. //Nevermind my comments on the bottom. Thanks a lot for taking your time.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Shaba</title>
</head>

<body>
<h1>
This is my Javascript site.
</h1>
<div id="output">
</div>
<script src="javascript.js" type="text/javascript"></script>
</body>
</html>
var info = [
  {name: "Abbas", track: "Front-End", achievments: 5, points: 7628},
  {name: "Tareq", track: "Front-End", achievments: 2, points: 7982},
  {name: "Faffar", track: "Design", achievments: 8, points: 2334},
  {name: "Jeorge", track: "Back-End", achievments: 7, points: 4233},
  {name: "Adel", track: "Databases", achievments: 5, points: 2342}
];
var infoString = [];
var infoName = [];
var infoTrack = [];
var infoAchievment = [];
var infoPoints = [];
var answer;

function print (message) {
      infoName = info[message].name;
      infoTrack = info[message].track;
      infoAchievment = info[message].achievments;
      infoPoints = info[message].points;
      infoString = "<div> Name: " + infoName + " Track: " + infoTrack + " Achievments: " + infoAchievment + " Points: " + infoPoints + "</div>";
      document.write(infoString)
}


while (true) {
  answer = prompt("What's the name?");
  if (answer.toLowerCase() === "quit" || answer.toLowerCase() === null) {
    break;
  }
  else {
    for (var i = 0; i < info.length; i++) {
      var object = info[i];
        if (object.name === answer.toLowerCase() || answer.toLowerCase() === null) {
        console.log("right");
        print(i);
        }
      }
    }
  }


/*
for (var i = 0; true; i++) {

  answer = prompt("What's the name?");
  if (answer.toLowerCase() === object.name.toLowerCase()) {
    if (toLowerCase(answer) === info[0].name ) {
      print(0);
    }
    else if (toLowerCase(answer) === info[1].name) {
      print(1);

  }
}
*/


/*
- loop prompt, input = studentname show info. fix so the loop ends when somebody writes quit.
-
    */

2 Answers

Ryan Gostic
Ryan Gostic
20,790 Points

You will receive an error if you try to call a method on a null object. First check if the object is null without calling .toLowerCase() then check for the correct value.

Marvin Benno
Marvin Benno
7,524 Points

How would you do that in code??

Marvin Benno
Marvin Benno
7,524 Points

Isn't that what I've done at line 27?

Marvin Benno
Marvin Benno
7,524 Points

Thanks man, appriciate the help!

Ryan Gostic
Ryan Gostic
20,790 Points

Change your first if statement to: if (answer === null) { break; }

Remove the check for null in the second if statement.

Ryan Gostic
Ryan Gostic
20,790 Points

if (answer === null || answer.toLowerCase() === "quit" ) { break; }