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 trialMarvin Benno
7,524 PointsQuestion 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
20,790 PointsYou 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.
Ryan Gostic
20,790 PointsChange your first if statement to: if (answer === null) { break; }
Remove the check for null in the second if statement.
Ryan Gostic
20,790 Pointsif (answer === null || answer.toLowerCase() === "quit" ) { break; }
Marvin Benno
7,524 PointsMarvin Benno
7,524 PointsHow would you do that in code??
Marvin Benno
7,524 PointsMarvin Benno
7,524 PointsIsn't that what I've done at line 27?
Marvin Benno
7,524 PointsMarvin Benno
7,524 PointsThanks man, appriciate the help!