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 trialDmitri Antonov
Courses Plus Student 6,010 PointsWhy code isn't working? Pls help...
Hi Guys,
here is the code:
var message = "";
//array of objects holding list of students
var student = [
{ name: "David", age: "23", course: "Java", level: "Guru" },
{ name: "Roman", age: "24", course: "HTML", level: "Professional" },
{ name: "Brigita", age: "28", course: "PHP", level: "intermediate" },
{ name: "John", age: "23", course: "HTML", level: "Rookie" }
];
function print(report) { var outputDiv = document.getElementById("output"); outputDiv.innerHTML = report; }
while(true) {
var input = prompt ("Please insert name of student..."); input = input.toLowerCase(); if (input === "quit") { break; }
for (var i = 0; i < student.length; i += 1) { if (input === student[i].name) { message += ("<h2>Student: " + student[i].name + "</h2>"); message += ("<p>Age: " + student[i].age + "</p>"); message += ("<p>Course: " +student[i].course + "</p>"); message += ("<p>Level: " + student[i].level + "</p>"); print(message); }
} }
what's wrong with it? When i type the name which is in list, loop ends and i receive empty page as output.
thx in adv
1 Answer
Anthony McCormick
6,774 PointsThe reason the page is not populating with any data, is because it cannot find what you're looking for.
input.toLowerCase() turns input into all lower case but the values in the object are uppercase. You need to also apply the .toLowerCase() method to what you find in the object. See my code below:-
while (true) {
var input = prompt("Please insert name of student...");
input = input.toLowerCase();
if (input === "quit") {
break;
}
for (var i = 0; i < student.length; i += 1) {
if (input === student[i].name.toLowerCase()) {
message += ("<h2>Student: " + student[i].name + "</h2>");
message += ("<p>Age: " + student[i].age + "</p>");
message += ("<p>Course: " + student[i].course + "</p>");
message += ("<p>Level: " + student[i].level + "</p>");
print(message);
}
}
}