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 trialKelly Musgrove
16,453 PointsMy code isn't working - please review?
Hello, I can't seem to get my code to work. I even followed along with Dave after a few hours of not getting the results I was looking for, and the code still doesn't seem to work. My original code was actually pretty close to his, but I can't get the student information to print to the page - it just keeps on looping unless I enter "quit". :-/
Any input would really be appreciated!
Thank you!!
var students = [
{
name: "Kelly",
track: "Front End Development",
achievements: 10,
points: 100
},
{
name: "Bethsy",
track: "JavaScript",
achievements: 5,
points: 50
},
{
name: "Lola",
track: "CSS",
achievements: 2,
points: 20
},
{
name: "Neo",
track: "HTML",
achievements: 1,
points: 10
},
{name: "Dave",
track: "Business",
achievements: 3,
points: 30
}
];
var message = "";
var student;
var search;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function getStudentReport(student) {
var report = "<h2>Student: " + student.name + "</h2>";
report += "<p>Track: " + student.track + "</p>";
report += "<p>Points: " + student.points + "</p>";
report += "<p>Achievements: " + student.achievements + "</p>";
return report;
}
while (true) {
search = prompt("Search student records: type a name [Jody] --- or type 'quit' to exit.");
if (search === null || search.toLowerCase() === "quit") {
break;
}
for (var i = 0; i < students.length; i += 1) {
student = students[i];
if (student.name === search) {
message = getStudentReport(student);
print(message);
}
}
}
3 Answers
Erik Nuber
20,629 PointsThis seems to be doing what it should. Just out of slight curiosity, when you test, are you typing in Jody? As that isn't a student on the list. I tested it and message equaled the student I wanted to print to screen. Outside of not having the html for it to place it; it should work.
The only other thing I can think of since it works up to that point, is that it is in your print function. That appears to be okay but, make sure you have an id="output" in your html.
Steven Parker
231,261 PointsThe code is designed to loop until Cancel or "quit".
So based on how it is written, that part is normal. Now without seeing the HTML part, the rest of this is a bit of a guess — but if the code is being run as part of the page loading process, nothing it does will become visible until it ends.
Also, the print function replaces the contents of the output area each time it runs, so when you quit, you should only see the result of the last successful search.
And if your HTML does not have an element with the id of "output", nothing will be seen.
Kelly Musgrove
16,453 PointsThanks Steven! Everything seems to be set up correctly - I do have an id of "output" in the HTML. If I search for something I know is in the list (i.e. "Kelly"), it just keeps on looping, without displaying any information. It doesn't print to the page, won't show in an alert, nor write to to console.
I'm not sure what's going on here on my end, as another member said the code worked for him :o/ I think I will have to test this somewhere outside of the network I'm on, as it's not working for me no matter what computer I'm using, or even if I use sublime text instead of Treehouse.
I really appreciate the response!
Kelly
James Bennett
3,085 PointsTry converting object names in "students" to lower case. Then try student.name === search.toLowerCase(). This is to make sure the student name matches up with the search variable.
I that doesn't work try, == rather than a strictly ===.
Kelly Musgrove
16,453 PointsKelly Musgrove
16,453 PointsHi Erik! Thanks so much for the quick response :-)
I am searching for names that are in the array of objects (Kelly, Lola, etc.), so I'm not sure what's going on. I tested this on three different browsers (safari, chrome, and firefox), on three different computers (two macs and a PC), and it doesn't work for me. Really weird!
I do have id="output" in the HTML as well.
Thanks again! Kelly
Erik Nuber
20,629 PointsErik Nuber
20,629 PointsCould you take a snapshot of your code. To do that you click on the camera within the workspaces. It is found in the top right hand corner. It will give you a link that can be shared.
Kelly Musgrove
16,453 PointsKelly Musgrove
16,453 PointsSure! Here you go:
https://w.trhou.se/fxzde3shej
All of the JavaScript is in the students.js file, as it was also giving me some issues earlier when trying to use multiple js files. (Sorry- should have removed the student_reports.js before I took the snapshot!)
Thanks again, Kelly
Erik Nuber
20,629 PointsErik Nuber
20,629 Pointsin students.js on line 29 you are using "alert" instead of print. change it and it works.
However, there are a couple of caveats.
1) you have to search exactly as it's spelled. ie Lola not lola. This can be fixed with toUpperCase() or toLowerCase()
2) You are simply replacing the student on screen. So when it prints to screen a name will appear but then disappear when you find the next student. This can be fixed by either building the message with += and printing after the search is done or creating an array to push students into and then later using a loop to print them back out etc.
Kelly Musgrove
16,453 PointsKelly Musgrove
16,453 PointsThanks again Erik, At the time I took the screenshot I was using the alert() method to test it, since the other print() method (and document.write()) wasn't working. The way you are describing it is definitely how it should work, it's just not working that way for me from where I'm sitting. It's kind of buggy now (working half way, only some of the times). I am thinking it must have something to do with the network I'm on, and not the code. Thank you so much for helping me troubleshoot though!
I really appreciate it :-)
Kelly