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

Another question regarding .toLowerCase().

In my code everything runs fine exept line 33 inside the last else statement. If I remove the .toLowerCase() the program runs fine. My question is, why is the .toLowerCase() method making the program not work? Want some insight on why this function seems a bit tricky? Thanks for 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 === null || answer.toLowerCase() === "quit") {
    break;
  }
  else {
    for (var i = 0; i < info.length; i++) {
      var object = info[i];
        if (object.name === answer.toLowerCase()) {
        console.log("right");
        print(i);
        }
      }
    }
  }
Ryan Gostic
Ryan Gostic
20,790 Points

You are also going to get an error since you are passing the integer index i into print instead of the object.

1 Answer

Ryan Gostic
Ryan Gostic
20,790 Points

Since your object.name is capitalized the if statement is never executing. You need to call .toLowerCase() on both answer and object.name.