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

Ivana Lescesen
Ivana Lescesen
19,442 Points

Is it possible to do this task by using a while loop and indexOf() method?

Amazing people at treehouse

Is it possible to do this task by using a while loop and indexOf() method?

And if not how can we check if the

search = prompt("Search for a student. Type a name of a student or 'quit' to exit");

is equal to

students.name

Thank you so much :)

2 Answers

Steven Parker
Steven Parker
231,269 Points

There's two issues with using indexOf in this situation.

  1. You would have to provide indexOf with the entire student object to match on, not just the name.
  2. indexOf only finds the first match, but the sample method finds all that have the same name.
Ivana Lescesen
Ivana Lescesen
19,442 Points

Steven Parker Thank you so much, what sample method? Would you advise against using index of in this particular task?

And if not how can we check if the

search = prompt("Search for a student. Type a name of a student or 'quit' to exit");

is equal to students.name by using the while loop Thank you so much

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'
  }
];


var html = " ";
var search;


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



while (true) {

  search = prompt("Search for a student. Type a name of a student or 'quit' to exit");
   search = search.toLowerCase();
  if ( search === 'quit') {
     break;
   } else if ( search === students.length) {
    print( search + ' is a student here'); 
  }
   else {
      print( search + ' is not a student'); 
    }
 }



for (var i = 0; i < students.length; i+= 1) {

    html += "<h2>Student: " + students[i].name + "</h2>";
    html += "<h3>Track: " + students[i].track + "</h3>";
    html += "<h3>Achievements: " + students[i].achievements + "</h3>";
    html += "<h3>Points: " + students[i].points + "</h3>";

}

print(html);
Steven Parker
Steven Parker
231,269 Points

I was referring to the sample method presented in the solution video.

It compares the search term with each student's name in a loop.

Instead of indexOf() you can use the .filter() method on your array that calls a function you create. It then passes each item/object from your array to your function where you should evaluate each item to true or false. For instance:

var array = [
  { name: 'John', points: 22 },
  { name: 'Rebecca', points: 33 },
  { name: 'Thomas', points: 32 },
  { name: 'Elise', points: 10}
];
var bestPlayers = [];

/*
* .filter() passes each object from array to the function 
* isScoreHighEnough. My function takes the object and 
* tests it against a condition. Any objects that test True 
* are returned and any that test false are ignored. The 
* array bestPlayers should now contain the two objects 
* with Rebecca and Thomas because their points were 
* greater than 30.
*
* Reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
*/
var bestPlayers = array.filter(isScoreHighEnough);


function isScoreHighEnough(player) {
  if(player.points > 30) {
    return true;
  } else {
    return false;
  }
}