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

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

I have this working sorta can someone help me?

I have this looping and I can change the student over and over until quit, however when i ask for a student that is not on the list only on first load of the page will it give "there is no student " but if if try and ask for another non existing student it wont change the same in the mssage but i can ask for someone that does exist and it will change to that person but after that if i ask for a non existing student again it leaves the last existing students records up and wont show the message there is no student that only works on the first try. can someone help me figure it out?

Program:

var message = '';
var student;
var search;

function print(message){
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function getStudentReport ( student ){
    var report = '<h2> Student: ' + student.name + '<h2>';
    report += '<p>Age: ' + student.age + '</p>';
    report += '<p>School: ' + student.school + '</p>';
    report += '<p>Lives: ' + student.lives + '</p>';
    return report;
}

while (true) {
  search = prompt( 'Type in a Name or type quit to exit');
  if (search === null || search.toLowerCase() === 'quit') {
  break;
  }
  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if ( student.name.toLowerCase() === search ){
    message = getStudentReport( student );

       }
  }
  if (message !== '') {
      print(message);

} else {
    message+= 'Student " ' + search + '" has no records';
    print(message);
}
}
  //---------------------------------------//
STUDENT RECORDS

var students = [
  {
    name:'Bradley',
    age: 33,
    school: 'Durham College',
    lives: 'Oshawa',
  },
  {
    name:'Ryan',
    age: 22,
    school: 'Durham College',
    lives: 'Oshawa',
  },
  {
    name:'Jeff',
    age: 29,
    school: 'Durham College',
    lives: 'Oshawa',
  },
  {
    name:'Sue',
    age: 44,
    school: 'Durham College',
    lives: 'Oshawa',
  }
];

can you please format your code so we can take a better look at your problem.

I have no idea why it did that lol it is formatted tho I will repost it thanks for letting me know I didn't notice it change on me like that...weird

1 Answer

rydavim
rydavim
18,814 Points

You are only updating your message when you find a student, or when the message is empty. However, after the first time through, the message will never be empty again. This is the root of the behavior you're seeing.

Your code will run as you expect if you reset the message variable to an empty string at the top of your while loop.

Please let me know if your issue still doesn't make sense. The rest of the code seems to run as expected.

Happy coding!