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 trialHosung Kim
11,243 PointsMy solution w/ bonus challenge solution.
let student;
let html = '';
let quit = false;
let search;
let noMatch = true;
let noStudentFound;
//Writes message to HTML
function print(message){
document.getElementById('output').innerHTML = (message);
}
//Lists the student record
function listStudent() {
html += `<h2>Student: ${student.name}</h2>`;
html += `<p>Track: ${student.track}</p>`;
html += `<p>Achievements: ${student.achievements}</p>`;
html += `<p>Points: ${student.points}</p>`;
}
//Loops through the student array object using the listStudent function until user types "quit"
while (!quit) {
search = prompt("Search student records: Type a name [ex: Steven] or type [quit] to exit.")
noMatch = true;
for(i = 0; i < students.length; i += 1) {
student = students[i];
if (search === student.name) {
noMatch = false;
listStudent();
} else if (search === "quit" || search === null){
quit = true;
break;
}
}
//If the searched name is NOT found in the student array object: Alert
if (noMatch && search !== null) {
alert("There is no record of that name.")
}
}
print(html);
2 Answers
Steven Parker
231,141 PointsLooks pretty good.
One further improvement would be to test the input for "quit" or empty before starting the loop to keep the loop shorter and more efficient. That would also eliminate the need for the "quit" variable.
Hosung Kim
11,243 PointsFinal Code
let student;
let html = '';
let search;
let noMatch;
let noStudentFound;
//Writes message to HTML
function print(message){
document.getElementById('output').innerHTML = (message);
}
//Lists the student record
function listStudent() {
html += `<h2>Student: ${student.name}</h2>`;
html += `<p>Track: ${student.track}</p>`;
html += `<p>Achievements: ${student.achievements}</p>`;
html += `<p>Points: ${student.points}</p>`;
}
//Loops through the student array object until user types "quit"
while (true) {
search = prompt("Search student records: Type a name [ex: Steven] or type [quit] to exit.")
noMatch = true;
if (search === "quit" || search === null){
break;
}
for(i = 0; i < students.length; i += 1) {
student = students[i];
if (search === student.name) {
noMatch = false;
listStudent();
}
}
//If the searched name is NOT found in the student array object: Alert
if (noMatch && search !== null && search !== "quit") {
alert("There is no record of that name.")
}
}
print(html);
Thank you for all the help and tips!
Hosung Kim
11,243 PointsSorry, I'm a little confused.. It was printing out each individual before the changes before adding the break after the 'for' loop. Wasn't that what you meant when you said to add a break after "noMatch = false"?
Steven Parker
231,141 PointsOh, what I had previously suggested was adding another "noMatch = false" before the other break. But then I realized it isn't necessary.
Hosung Kim
11,243 PointsAh ok. Thanks for all your help!
Hosung Kim
11,243 PointsHosung Kim
11,243 PointsHey Steven. Thank you for the critique! I think I understood what you were saying correctly(?) Did you mean like this?
Steven Parker
231,141 PointsSteven Parker
231,141 PointsYes, that should do it!