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 trialKarina Gubaidullina
Courses Plus Student 8,318 PointsI did it!!! My solutions... Do you like them?
var input;
var student;
var studentName;
var message = '';
var noStudent = '';
var counter = 0;
var i = 0;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
while (true) {
input = prompt('Type the name of student or type \"quit\" to cancel');
counter = 0;
i = 0;
if ( input.toUpperCase() === 'QUIT' || input === null ) {
break;
}
while ( i < students.length ) {
student = students[i];
studentName = student.name;
if ( input.toUpperCase() === studentName.toUpperCase() ){
for ( var key in student ) {
if ( key === 'name' ) {
message += '<h2>Student: ' + studentName + '</h2>';
} else {
message += '<p>'+ key + ': ' + student[key];
}
}
} else {
counter += 1;
if ( counter === students.length ) {
message += '<h2>No student with the name ' + input + '</h2>';
}
}
print(message);
i += 1;
}
}
2 Answers
Alexander Davison
65,469 PointsThe code is difficult to read, as it is very deeply nested.
I recommend splitting the code into different functions.
Noah Schade
17,694 PointsYou should switch around the order of: if ( input.toUpperCase() === 'QUIT' || input === null ). It should be: if ( input === null || input.toUpperCase() === 'QUIT' ). I'm getting this error message when I run your code in the browser: Uncaught TypeError: Cannot read property 'toUpperCase' of null.
Roger Hwang
3,851 PointsRoger Hwang
3,851 PointsGood reminder Alex. Clearly named functions that describe their purposes is good practice.