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 trialBong Chon
1,277 PointsFor the Student Search Record Challenge, can you please tell me why my script doesn't respond when I type in a name?
I hope the link above works to show a snapshot of my workspace. Whenever I type in a valid name, like "Trish," my program doesn't respond. I think it has to do with my print function, but I am not sure what is wrong with it as no error shows in the console log. My break statement still works along with typing in an invalid input. I did approach this differently from how the tutorials instructed, but the way I did it still looks correct on surface to me.
3 Answers
Zak Posner
8,473 PointsI see a few problems with your code.
1) Your print function does not actually print anything. All it does is return the value to be used by whatever calls the function. So you are merely saving that value for internal use, rather than actually printing it to the page for the user to see. Use the .getElementByID method outlined in the course, or just change the return message to document.write(message)
2) Consider using a loop to avoid typing out the exact same line of code for each name condition. Remember DRY. Below is how I accomplished the task, maybe that will help you:
var searchActive = true;
var searchPrompt;
function printStudent (student) {
var studentInfo = "<h2> Student Name: " + students[student].name + "</h2>";
studentInfo += "<p> Track: " + students[student].track + "</p>";
studentInfo += "<p> Achievements: " + students[student].achievements + "</p>";
studentInfo += "<p> Points: " + students[student].points + "</p>";
document.write(studentInfo);
}
function search(name) {
for (i = 0; i < students.length; i++) {
if (name === students[i].name) {
printStudent(i)
}
}
}
while (searchActive) {
searchPrompt = prompt("Search for any student in the system by name, or type quit to exit");
search(searchPrompt);
if (searchPrompt.toLowerCase() === "quit") {
searchActive = false;
}
}
Steven Parker
231,533 PointsYour print function returns a string, but the places it is called from do not assign or otherwise use the returned value.
One way to see the result is if you were to replace the "return message
" in print with something like this:
document.getElementById("output").innerHTML = message;
Happy coding! -sp
Bong Chon
1,277 PointsHi Steven,
Thank you for your prompt response. I tried your suggestion, and that did work. If you don't mind, can you explain why the 'place' that it is calling from does not use the returned value?
Thank you again.
Bong Chon
1,277 PointsBong Chon
1,277 PointsAh now that it has been several days, it is more clear now why. Thank you so much.