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
Yiran Wu
2,803 PointsTried to print out if we don't have certain students, instead, that's all it prints out. - Student Record Challenge.
var student; var record = " "; var request = " ";
function print(message) { var list = document.getElementById("output"); list.innerHTML = message; }
function studentRecord(student) { var report = "<h2> Student: " + student.name + "</h2>"; report += "<p> Track: " + student.track + "</p>"; report += "<p> Achievement: " + student.achievement + "</p>"; report += "<p> Points: " + student.points + "</p>"; return report; }
// search for student record while (true) { request = prompt("Type in a student's name for the student's record. Type \"list\" for all students name. Type \"quit\" to exit the search.");
if (request === null || request === "quit") { break; } else if (request === "list") { for (var i = 0; i < students.length; i++) { student = students[i]; record += "<h2> Student: " + student.name + "</h2>"; record += "<p> Track: " + student.track + "</p>"; record += "<p> Achievement: " + student.achievement + "</p>"; record += "<p> Points: " + student.points + "</p>"; print(record); } }
for (var i = 0; i < students.length; i ++){ student = students[i]; if (student.name === request) { message = studentRecord(student); print(message); } else if (student.name.indexOf(request) === -1) { print("Sorry, we don't have a student named: " + request); } } }
Very last else if statement. I tried to print out if the search is not in our database. However, after I added that else if statement, even I typed the students I have in the database, it would print out "Sorry, we don't have a student named: " + request.
Not sure what's wrong with my code. Also tried just an else statement, same way.
1 Answer
Andrew Young
Courses Plus Student 639 PointsHere is my code (jsfiddle)
var students = [{
name: "andrew",
track: "1",
"achievement": "helloworld",
"points": 23
}];
var record = " ";
var request = " ";
function print(message) {
var list = document.getElementById("output");
list.innerHTML = message;
}
function studentRecord(student) {
var report = "<h2> Student: " + student.name + "</h2>";
report += "<p> Track: " + student.track + "</p>";
report += "<p> Achievement: " + student.achievement + "</p>";
report += "<p> Points: " + student.points + "</p>";
return report;
}
// search for student record
//while (true) {
// delete while statement cause you don't have break when you show result and that will cause infinte loop!
request = prompt("Type in a student's name for the student's record. Type \"list\" for all students name. Type \"quit\" to exit the search.");
/*if (request === null || request === "quit") {
//break;
} else */
// delete this part because it's useless without while
if (request === "list") {
for (var i = 0; i < students.length; i++) {
student = students[i];
record += "<h2> Student: " + student.name + "</h2>";
record += "<p> Track: " + student.track + "</p>";
record += "<p> Achievement: " + student.achievement + "</p>";
record += "<p> Points: " + student.points + "</p>";
print(record);
}
}
for (var i = 0; i < students.length; i++) {
student = students[i];
if (student.name === request) {
message = studentRecord(student);
print(message);
} else if (student.name.indexOf(request) === -1) {
print("Sorry, we don't have a student named: " + request);
}
}
//}
See the comments in the script I told you where I change and why I change it!
Cheers, Andrew
Andrew Young
Courses Plus Student 639 PointsAndrew Young
Courses Plus Student 639 PointsPlease, just please add ```js before your code and ``` after your code, it'll make it easier to look :)