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 trialIkechukwu Arum
5,337 Pointsmy code does not produce the desired result. how can I fix this?
it doesn't print the student records . why?
var students = [
{
name: 'Dave',
track: 'Front End Development',
achievements: 158,
points: 14730
},
{
name: 'Jody',
track: 'iOS Development with Swift',
achievements: '175',
points: '16375'
},
{
name: 'Jordan',
track: 'PHP Development',
achievements: '55',
points: '2025'
},
{
name: 'John',
track: 'Learn WordPress',
achievements: '40',
points: '1950'
},
{
name: 'Trish',
track: 'Rails Development',
achievements: '5',
points: '350'
}
];
let message;
let search;
function printToScreen(message){
let outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function promptUser (){
search = prompt("Enter student name");
return search;
}
function studentReport(student){
let report = `<h4>Student: ${student.name}</h4>`;
report += `<p> Track: ${student.track}
<br> Achievements:${student.achievements}
<br> Points: ${student.points}
</p>`;
return report;
}
while(true){
search =promptUser();
if(search ===null || search.toLowerCase() === 'quit'){
message = `<p>Thanks.Goodbye! </p>`;
printToScreen(message);
break;
}
for(let i = 0; i<students.length; i++){
if(search.toLowerCase() === students[i].name.toLowerCase()){
printToScreen(studentReport(students[i]));
}
else{
message = `<p> Student ${search} does not exist. Try Again!</p>`;
printToScreen(message);
}
}
}
4 Answers
Steven Parker
231,269 PointsThe "printToScreen" method completely replaces the content of the output element, so no matter what else happens only "Thanks.Goodbye!" will be visible when the program completes.
But also this code only checks the very first student ("Dave"). If the search does not match, the loop ends (because of the "break") and no other students are checked. The loop should be allowed to run through all the student records before considering the search to fail.
Ikechukwu Arum
5,337 Pointsyeah but i still stuck trying to print one student and i don't know what i am doing wrong...and not sure what you mean but "add to instead of replace the output text". could you explain?
Ikechukwu Arum
5,337 Pointsok i understand. However my issue right now is i don't understand why it won't print student information that matches.
using "Dave" as a test case, nothing displays on the screen until I cancel the prompt. when I cancel the prompt, it goes back into the first if
block and prints "Thanks. Goodbye!" to the screen. I just want it to print the student details if the name matches
Steven Parker
231,269 PointsSee the important Update in the Teacher's Notes below the next video that explain why nothing happens until the program ends. Then the two reasons I already gave explain why only "Thanks.Goodbye!" will be visible.
Ikechukwu Arum
5,337 PointsThanks. I fixed it. The teachers notes probably needs to be updated to reflect an alternative approach to solving this problem.
let message;
let search;
function printToScreen(message){
let outputDiv = document.getElementById('output');
outputDiv.innerHTML += message;
}
function promptUser (){
search = prompt("Enter student name");
return search;
}
function studentReport(students){
if(students.length ===0 || students === null){
message = `<p> Student ${search} does not exist. Try Again!</p>`;
printToScreen(message);
}
let report ="";
for(let student in students){
report += `<h4> Student: ${students[student].name} </h4>
<p> Track: ${students[student].track}
<br> Achievements: ${students[student].achievements}
<br> Points: ${students[student].points}
</p>`;
}
printToScreen(report);
}
while(true){
search =promptUser();
let arrayOfStudents = [];
if(search ===null || search.toLowerCase() === 'quit'){
//message = `<p>Thanks.Goodbye! </p>`; printToScreen(message);
break;
}
for(let i = 0; i<students.length; i++){
if(search.toLowerCase() === students[i].name.toLowerCase()){
arrayOfStudents.push(students[i]);
}
}
studentReport(arrayOfStudents);
}
Ikechukwu Arum
5,337 PointsIkechukwu Arum
5,337 Pointsi was just trying to follow the tutorial. but i have updated it. My problem is that is wouldn't print the student details as expected. and i am not sure why. something is wrong with my if statement in the for loop. it refuses to execute the printscreen even when i have the right student
Steven Parker
231,269 PointsSteven Parker
231,269 PointsIf you want to display more than one message, you need to add to instead of replace the output text.
And you need to revise how the loop works to be able to find any student other than the first.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsI'm referring to this line in "printToScreen":
outputDiv.innerHTML = message;
This statement replaces what is being shown on the page with the argument passed to the function. This means that no matter how many times you call the function, only what was passed in the last call will be seen. You could add to the output instead of replacing it by using a different operator:
outputDiv.innerHTML += message;