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
William Vauter
Courses Plus Student 4,536 PointsSuggestions for final Loops, Arrays, and Objects Challenge?
I have been working on this code for a while, and after reading through the other posts on the challenge I can't figure out how to fix my code. The prompt does not change if the user enters a student name not in the system. How could I fix this?
var message = '';
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) { var studentSearch = prompt('Enter the name of a student to see their records. Type "quit" to quit.'); if (studentSearch === null || studentSearch.toLowerCase() === 'quit') { break; } else if (studentSearch === undefined) { studentSearch = prompt('No student was found. Enter a valid student name.'); } else { for (var i = 0; i < students.length; i += 1) { var student = students[i]; if (studentSearch.toLowerCase() === student.name.toLowerCase()) { print(getStudentReport(student)); } } } }
2 Answers
Brandon VanCamp
Front End Web Development Techdegree Graduate 30,954 PointsAlternatively, typeof can be used:
var x;
if (typeof x === 'undefined') {
// these statements execute
}
One reason to use typeof is that it does not throw an error if the variable has not been declared.
kevin anderson
Front End Web Development Techdegree Graduate 41,112 PointsIt appears that your else if statement that is supposed to give prompt to search for a valid name does not actually loop through the list of student names..need to loop through this array or object ton actually see if name is on list or not...hope this helps
William Vauter
Courses Plus Student 4,536 PointsYeah, I wasn't sure why the prompt doesn't change if the name entered is invalid. Any suggestions on how to fix that?
kevin anderson
Front End Web Development Techdegree Graduate 41,112 PointsThe else if statement for undefined needs to be inside the else statement after your for loop...so for loop should havr an if and else statement..