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

Cant find my mistake, Everything looks fine to me but the problem is it wont exit & it wont show me particular student.

Cant find my mistake, Everything looks fine to me but the problem is write "exit" it wont exit and when i search for the particular student name and wont show me the student instead it will ask me the same question again. Help please,

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>Track: ' + student.track + '</p>';
  report = '<p>Points: ' + student.points + '</p>';
  report = '<p>Achievements: ' + student.achievements + '</p>';
  return report;

}


while (true) {
  search = prompt ('Search student records: type a name [Jody] (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 === search ){
    message = getStudentReport (student);
    print(message);
    }
  }
}

Moderator edited: Markdown added so that code renders properly in the forums. -jn

Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,696 Points

Question updated with code formatting. Check out the Markdown Cheatsheet link below the "Add an Answer" for syntax examples.

4 Answers

Dean Paterson
Dean Paterson
12,996 Points

Just a quick scan at the code. The report var is assigned at the start then other statements added to the same variable. By adding more statements you should use +=. Not just =. As your adding to (+=) not reassigning (=)

I have put (report +=), now it works when i say quit but still it wont bring the particular student report when i search by there name. Thanks alot though

Dean Paterson
Dean Paterson
12,996 Points

What does your students.js file look like?

This is my students.js same as the video.

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'
  }
];
M Baker
seal-mask
.a{fill-rule:evenodd;}techdegree
M Baker
Full Stack JavaScript Techdegree Student 5,983 Points

you're missing a closing bracket on your while loop: TRY:

while (true) { search = prompt ('Search student records: type a name [Jody] (or type "quit" to exit)'); if (search === null || search.toLowerCase() === "quit"){ break; } }

Also, personal preference here, but I like to make a var quit = false; and use it as a second catch to exiting. I cannot speak to if this is good coding or not but it gives me a warm fuzzy. Like this:

var quit = false; while (!quit) { search = prompt ('Search student records: type a name [Jody] (or type "quit" to exit)'); if (search === null || search.toLowerCase() === "quit"){ quit = true; break; } }

This way it can't re-enter the loop after 'quit' has triggered the quit variable to be false. Just my 2 cents

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>Track: ' + student.track + '</p>';
// Replaced report = with report +=
    report += '<p>Points: ' + student.points + '</p>';
    report += '<p>Achievements: ' + student.achievements + '</p>';
    return report;

}


while (true) {
    search = prompt('Search student records: type a name [Jody] (or type "quit" to exit)');
    if (search === null || search.toLowerCase() === "quit" || 'exit') {
// you were typing 'exit' but the exit string was nowhere in your code so i added OR 'exit'
        break;
    }
  }
//you had the for loop inside the while loop
    for (var i = 0; i < students.length; i += 1) {
        student = students[i];
        if (student.name === search) {
            message = getStudentReport(student);
            print(message);
        }
    }

This Should be the right answer