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

Muhammad Athar
Muhammad Athar
4,004 Points

Need help with my solution

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

function getReport( student ){
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}


while(true){

  search = prompt("Search for a student by name or type 'quit' to exit");
  console.log(search);
  if( search === null || search.toLowerCase() === 'quit' ){
    break;
  } else {
    for (var i = 0; i < students.length; i += 1) {
      student = students[i];
      if( search.toLowerCase() === student.name.toLowerCase() ) {
        console.log("matched");
        message += getReport( student );
        print(message);
      } else {
        print("<h2>There is no student by that name</h2>");
      }
    }  
  }
  message = '';
} 

I can't understand why the search would only work with Jody and not the other students. Whereas, the console.log message shows that the searhc matches. Please help me fix this

4 Answers

Ali Raza
Ali Raza
13,633 Points

Please declare a student variable. In order to assign the looped students value and log them, you need to assign them to a variable. I see you declared message variable but no student variable and search variable.

I see this variable:- message += getReport( student );

But I don't see it declared any where

Try putting: var message = '';

At the beginning of the while loop

and remove the: message = ''

At the bottom of it

Muhammad Athar
Muhammad Athar
4,004 Points
function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function getReport( student ){
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}


while(true){
  var message = "";
  var student;
  var search = "";
  search = prompt("Search for a student by name or type 'quit' to exit");
  if( search === null || search.toLowerCase() === 'quit' ){
    break;
  } else {
    for (var i = 0; i < students.length; i += 1) {
      student = students[i];
      if( search.toLowerCase() === student.name.toLowerCase() ) {        
        message += getReport( student );
        print (message);
      } else if (search.toLowerCase() !== student.name.toLowerCase()){
        print ("Not Found Please Try Again");
      }
    }  
  }
} 

Still doesn't work. It only shows the result for Jody. If I remove the else statement then it works. Please help.

Robert Szabo
Robert Szabo
7,999 Points

Hey Muhammad, I managed to get your script alive, let me show you how I did it. I changed a couple of things. This one works.

var message = "";
var student;
var search;

function getReport( student ){
  var report = '<h2>Student: ' + student.name + '</h2>';
  report += '<p>Track: ' + student.track + '</p>';
  report += '<p>Points: ' + student.points + '</p>';
  report += '<p>Achievements: ' + student.achievements + '</p>';
  return report;
}

while(true){
  search = prompt("Search for a student by 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( search.toLowerCase() === student.name.toLowerCase() ) {        
      message = getReport( student );
      print (message);
    }
  }
  if ( message === "" ) {
        print("Not Found Please Try Again");
  }
  message = ""; //With this we reset the message variable after each search.
} 

And here is your original, I commented what I've changed. Some logical explanation: When you ran the for loop, the code goes into the array and checks each object within it one by one if it passes with the user input. And stores the last value in the student.name.toLowerCase()//=students[i].name.toLowerCase()//, so actually this one will have the same value all the time, the last checked object, which is the last object in the array since the code runs in order. This is why it's better to the message variable. If there is nothing in the message variable that means the code did not find anything within the array what equals with the user search and it did not print out anything. So this if statement will be true and print out the try again message. If there is printed out something before than its value will be false so will not print anything. We also need to reset the message variable at the end of the while loop so when user searches again, there will be nothing as default in the message variable so our lines will run correctly.

while(true){
  var message = "";   //I would place these variables outside of the while loop, not sure though that we must or not.
  var student;
  var search = "";
  search = prompt("Search for a student by name or type 'quit' to exit");
  if( search === null || search.toLowerCase() === 'quit' ){
    break;
  } else { //We don't need an else statement, the for loop is enough but place it with the same level with the first if.
    for (var i = 0; i < students.length; i += 1) {
      student = students[i];
      if( search.toLowerCase() === student.name.toLowerCase() ) {        
        message += getReport( student );
        print (message);
      } else if (search.toLowerCase() !== student.name.toLowerCase()){ //This one also goes to the first if's level.
        print ("Not Found Please Try Again");
      }
    }  
  }
}  //There are one more brackets than necessary.

Please note that I'm just learning also so my solution is probably not the best, but works. :)