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

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

can some 1 tell me whats wrong with this code?

my problem is with the alert box for the name not found, if i type in any student name to search , "name not found "alert box apprears 1st and the on typing quit, it shows the name if it was in the list. but not for student name "dave" ! it doesnt follow with the alert box.....for dave it just shows the student record when i type in quit,why? i just want it to appear only when "student.name.toLowerCase() != search.toLowerCase()" plus for matching names with no effort its just showing me all the matching name records when i type in quit in the end. dont know why, even tho it solves the challenge for me but dont know how? so its no gud..... HELP PLEASE... m stuck on this challenge frm last 2 days. thanks in advance!!

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
  },
   { name: 'Jody',
    track: 'web Development',
    achievements: 15,
  points: 750
  }
  ];
var message = '';
var student;
var search;

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

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

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.toLowerCase() === search.toLowerCase() ){
      print(getStudentRecord( student ));
     }
   } 
  for (var i = 0; i<students.length; i+=1 ){
    student = students[i];
  if (student.name.toLowerCase() != search.toLowerCase()){
     alert('name not found');}
  break;
  }
 }

1 Answer

Steven Parker
Steven Parker
229,732 Points

It's normal for modern browsers to not render the page content until after the JavaScript program has finished running. This is a behavior change from the concurrent operation that was common back when the video was produced, and is explained in the Teacher's Notes section at the bottom of the video page.

Your other issue is caused by having two separate search loops, the first prints a record if found, and the second one shows the alert if any student does not match the search name. And since the second loop has a break after the first test, this message is shown for all searches for names other than "Dave" (even if it was found by the first loop).

You'll probably want to use just one search loop, and after the loop finishes show the "not found" message only if the loop was not successful.