Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge Solution

Arianna Saoirse
Arianna Saoirse
10,078 Points

Why does every name return the 'not a student here' message?

All of the code here seems to work except the missing student message. That message prints whether the student is listed in the array or not.

var message = '';
var student;
var search;
var position = students.indexOf(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 (ex. = 'Dave') (or type 'quit' to end).");

  if ( search === null || search.toLowerCase() === 'quit') {
    break;
  } else if (search === "" || search === " " ){
    alert("Please enter a name");
  } else if ( position < 0 ) {
      alert("I'm sorry, '" + search + "' is not a student here. Please make sure the name is correct and try again.");           
  } 

  for (var i = 0; i < students.length; i += 1) {
    student = students[i]
    if ( student.name.toUpperCase() === search.toUpperCase()  ) {
      message = getStudentReport( student );
      print (message);
    }
  }
}

2 Answers

Steven Parker
Steven Parker
210,423 Points

There's no definition for "students". But even if it is defined elsewhere, when "position" is assigned on line 4, "search" was just created and is still undefined. So the result of "indexOf" will be -1.

Then later, since position is always less than 0, the message is always shown unless the input is empty or "quit". Even when a name is given, "position" is never updated.

Arianna Saoirse
Arianna Saoirse
10,078 Points

Thanks for your answer. Sorry, I should have included more information.

This is the code in it's entirety: https://w.trhou.se/ek2a0svmdv>

I forgot to mention that I also tried defining "position" in the while loop, with the same result:

while (true) {
  search  = prompt("Search student records: type a name (ex. = 'Dave') (or type 'quit' to end).");
  position = students.indexOf(search);
  if ( search === null || search.toLowerCase() === 'quit') {
    break;
  } else if (search === "" || search === " " ){
    alert("Please enter a name");
  } else if ( position < 0 ) {
      alert("I'm sorry, '" + search + "' is not a student here. Please make sure the name is correct and try again.");           
  } 
  for (var i = 0; i < students.length; i += 1) {
    student = students[i]
    if ( student.name.toUpperCase() === search.toUpperCase()  ) {
      message = getStudentReport( student );
      print (message);
    }
  }
}

(edit:) I have realized this is because it is an array of objects and the string returned does not match all of the properties in the object. So am I better changing my approach to this challenge, or should I create a for loop based on the search to find the object in the array which would return the correct position value?

Arianna Saoirse
Arianna Saoirse
10,078 Points

Ended up with this code which seems to work and didn't require much change:

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 (ex. = 'Dave') (or type 'quit' to end).");
  if ( search === null || search.toLowerCase() === 'quit') {
    break;
  } else if (search === "" || search === " " ){
    alert("Please enter a name");
  }

  var position =  -1;
  for (var i = 0; i < students.length; i += 1) {
    student = students[i]
    if ( student.name.toUpperCase() === search.toUpperCase()  ) {
      message = getStudentReport( student );
      print (message);
      position = i;
      break;
    }
  }

  if ( position < 0 ) {
      alert("I'm sorry, '" + search + "' is not a student here. Please make sure the name is correct and try again.");           
  } 
}

Thanks for your help!