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

Shreyas Sreenivasa
Shreyas Sreenivasa
7,804 Points

Part 1 of extra challenge not working.

I'v gone through the code many times but nothing seems to be wrong. Anything I type it says Nothing seems to be found.

My code:

var students = [
  { 
    name: 'Shreyas',
    track: 'JavaScript',
    achievments: 100,
    points: 5000,
  },
  {
    name: 'Sonia',
    track: 'Front End Development',
    achievments: 0,
    points: 0,
  },
  {
    name: 'Viraj',
    track: 'Java',
    achievments: 100,
    points: 5000,
  },
  {
    name: 'Vishnu',
    track: 'IOS Development with Swift 2.0',
    achievments: 200,
    points: 6000, 
  },
  {
    name: 'Sankalp',
    track: 'C#',
    achievments: 100,
    points: 4000,
  },
];

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> Achievments: ' + student.achievments + '</p>';
  report += '<p> Points: ' + student.points + '</p>';
  return report;
}

while(true) {
  search = prompt('Type a name: ');

  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 = getStudentReport(student);
      print(message);
    } else if (student.name.toLowerCase() !== search.toLowerCase() ) {
      message = "<h2>No student name was found</h2>";
      print (message);
    }
  } 
}

1 Answer

Justin Coen
seal-mask
.a{fill-rule:evenodd;}techdegree
Justin Coen
Full Stack JavaScript Techdegree Student 12,829 Points

The problem is that your for loop is iterating through each student one at a time to be matched up against, rather than all the students at once.

The code will only work with the first student in the array - 'Shreyas', since the first iteration of student[i] == student[0]

Then after the first input is made into the prompt the code stops, limiting you to only check the input against student[0]

Shreyas Sreenivasa
Shreyas Sreenivasa
7,804 Points

But it doesn't work with 'Shreyas' also.

Justin Coen
seal-mask
.a{fill-rule:evenodd;}techdegree
Justin Coen
Full Stack JavaScript Techdegree Student 12,829 Points

@Shreyas Sreenivasa, that is odd, I logged out the responses and it seems to be working for me when I put it in code pen

  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    console.log(students[i]);
    if (search.toLowerCase() === student.name.toLowerCase()) {
      console.log('Found');
      message = getStudentReport(student);
      print(message);
    } else if (student.name.toLowerCase() !== search.toLowerCase() ) {
      console.log('Not found');
      message = "<h2>No student name was found</h2>";
      print (message);
    }
  }

but it only comes back as 'Found' for Shreyas