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

Student Record Challenge

Please help me improve my code, and figure out how to display duplicate names, I honestly have no clue where to start.

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: 'Jody', track: 'Learn WordPress', achievements: 40, points: 1950 },
  { name: 'Trish', track: 'Rails Development', achievements: 5, points: 350 }
];

var htmlMessage = '';
var student;
var search;
var notFound = [];

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 end)');
  if (search === null || search.toLowerCase() === 'quit') {
    print('<p> These names were not found : ' + notFound.join(', ') + '</p>');
    break;
  }
  for (var i = 0; i < students.length; i++) {
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase()) {
      htmlMessage = getStudentReport( student );
      print(htmlMessage);
      break;
    } else if (students.indexOf(search) -1){ // (i === (students.length) -1)
        print('<p>' + search + ' was not found.</p>');
        notFound.push(search);
        break;
    }
  }
}

4 Answers

Good enough. If you want to improve something maybe you would like to not print "students.length" times "was not found" and store for nothing notFound.push(search) array in case user typed quit as first prompt answer?

Also, you can make your loop working even error/null is typed or cancel pressed and will not break cycle on successful name search?

I saw many forum posts with this quizz and yours have a firm one looking. What is your first programming language?

Hey Daniel, thanks for the help. I just realized I had a bug in my code. I changed the condition in my else if statement to the following: i === (students.length) -1, but I still don't understand completely what is going on here. I would appreciate it if you could explain this to me.

Oh and my first programming language is Python, but I'm just a newbie.

students.indexOf(search) -1)

It's about getting index number 1...n and decrease it, to get index of array students. If it first one student then you index will be 0 and all if statement become false. But in fact, indexOf return values 0...n and you don't need to decrease it. Look at the documentation.

if (students.indexOf(search) > -1 )

Thanks Daniel! I understand it now.