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

A bit lost on this one -- help appreciated

So, this is what I've got so far. I've changed some of the terms around to make things more interesting for myself, but the structure's the same. Searching for a player only works for the first player, and I always get my 'No such player exists. Please try again.' alert. Can anyone shed some light here?

// Declaring Variables
var html = '';
var player;
var search;

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

function getPlayerReport() {
  var report = '<h2>Player: ' + players[i].name + '</h2>';
  report += '<p>Position: ' + players[i].position + '</p>';
  report += '<p>Kit Number: ' + players[i].kitNumber + '</p>';
  report += '<p>Height: ' + players[i].heightCM + 'cm </p>';  
  return report;
}

// Loop for.ever
while (true) {
  search = prompt("Enter a player's last name to search for a player. Type 'quit' to quit.");
  if (search === null || search.toLowerCase() === 'quit') {
   break;
   }
   for (var i = 0; i < players.length; i++) {
     player = players[i];
     if (player.name.toLowerCase() === search.toLowerCase()) {
     html = getPlayerReport(player);
     print(html);
    }
    if (player.name.toLowerCase() !== search.toLowerCase()) {
      alert('No such player exists. Please try again');
      break;
    }
   }
}

Thanks!

Hi Rich-

I'm testing this now, and getting " 'players' not defined" (need to declare as global variable). Also, you are needing to create an object with player info.

Ack! Sorry about that, Greg. I forgot about that bit. The relevant code's below:

var players = [
  {
    name: "Fahrmann",
    position: "goalkeeper",
    kitNumber: 1,
    heightCM: 196, 
  },
  {
    name: "Geis",
    position: "midfielder",
    kitNumber: 5,
    heightCM: 181, 
  },
  {
    name: "Meyer",
    position: "midfielder",
    kitNumber: 7,
    heightCM: 173, 
  },
  {
    name: "Goretzka",
    position: "midfielder",
    kitNumber: 8,
    heightCM: 189, 
  },
  {
    name: "Sane",
    position: "midfielder",
    kitNumber: 19,
    heightCM: 183
  }
];

Thanks!

3 Answers

Here's a working version of that:

var players = [{
    name: "Fahrmann",
    position: "goalkeeper",
    kitNumber: 1,
    heightCM: 196,
}, {
    name: "Geis",
    position: "midfielder",
    kitNumber: 5,
    heightCM: 181,
}, {
    name: "Meyer",
    position: "midfielder",
    kitNumber: 7,
    heightCM: 173,
}, {
    name: "Goretzka",
    position: "midfielder",
    kitNumber: 8,
    heightCM: 189,
}, {
    name: "Sane",
    position: "midfielder",
    kitNumber: 19,
    heightCM: 183
}];

var search;

// Consolidated your getPlayerReport() and print() functions into this one
// Also made it so that it appends the new report to the old one — rather than replace the old one
function printReport(player) {
    var report = '<h2>Player: ' + player.name + '</h2>';
    report += '<p>Position: ' + player.position + '</p>';
    report += '<p>Kit Number: ' + player.kitNumber + '</p>';
    report += '<p>Height: ' + player.heightCM + 'cm </p>';

    document.getElementById('output').innerHTML += report;
}

// Checks if there exists a player by a given last name
function isPlayer(lastName) {
    for (var i = 0; i < players.length; i++) {
        if (players[i].name.toLowerCase() === lastName.toLowerCase()) {
            return true;
        }
    };
    return false;
}

// Gets the player's index in the players array
// Wouldn't be necessary if your data was an object of objects rather than an array of objects but whatevs haha
function getPlayerIndex(lastName) {
    for (var i = 0; i < players.length; i++) {
        if (players[i].name.toLowerCase() === lastName.toLowerCase()) {
            return i;
        }
    };
}

while (true) {
    search = prompt("Enter a player's last name to search for a player. Type 'quit' to quit.");
    if (search === null || search.toLowerCase() === 'quit') {
        break;
    }

    if (!isPlayer(search)) {
        alert('No such player exists. Please try again');
        continue;
    }

    printReport(players[getPlayerIndex(search)]);
}

Let me know if you have any questions. As for your original code, the only thing really messing it up was that second if-block inside of the for-loop ... inside of the while-loop.

Looks like the issue lies in a few areas. I got this to work if you remove the last If statement - fix that and you are all set. Note the differences in consistency between message/html and player/players. Hope this helps.

// Declaring Variables

var players = [
  {
    name: "Fahrmann",
    position: "goalkeeper",
    kitNumber: 1,
    heightCM: 196, 
  },
  {
    name: "Geis",
    position: "midfielder",
    kitNumber: 5,
    heightCM: 181, 
  },
  {
    name: "Meyer",
    position: "midfielder",
    kitNumber: 7,
    heightCM: 173, 
  },
  {
    name: "Goretzka",
    position: "midfielder",
    kitNumber: 8,
    heightCM: 189, 
  },
  {
    name: "Sane",
    position: "midfielder",
    kitNumber: 19,
    heightCM: 183
  }
];

var html = '';
var search;
var player;

// Some functions
function print(html) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = html;
}


function getPlayerReport(player) {
  var report = '<h2>Player: ' + player.name + '</h2>';
  report += '<p>Position: ' + player.position + '</p>';
  report += '<p>Kit Number: ' + player.kitNumber + '</p>';
  report += '<p>Height: ' + player.heightCM + 'cm </p>';  
  return report;
}


// Loop for.ever
while (true) {
  search = prompt("Enter a player's last name to search for a player. Type 'quit' to quit.");
  if (search === null || search.toLowerCase() === 'quit') {
   break;
   }
   for (var i = 0; i < players.length; i += 1) {
     player = players[i];
     if (player.name.toLowerCase() === search.toLowerCase()) {
     html = getPlayerReport(player);
     print(html);
    }
   }
}
Casey Ydenberg
Casey Ydenberg
15,622 Points

You're calling getPlayerReport with the argument player but you haven't set up the function to accept any arguments. Within the function body, you refer to players[i] instead of player, and i isn't defined at all in that scope. Clean that up and it should work.

I would also replace the second if statement with else, since the condition you've defined is the logistic opposite of the first if statement. If one or the other will always execute but never both, you might as well make it crystal clear to your future self.