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 trialBrian Patterson
19,588 PointsCan someone explain in plain English what is going on?
Can someone explain in plain English what is going on with this function?
//Passing in the player and coordinates
function checkForShip (player, coordinates) {
//location of shipPresent not sure what ship is doing here?
var shipPresent, ship;
//Can we use a forEach loop here and how would it be implemented.
//So for every ship we have
for (var i = 0; i < player.ships.length; i++ ) {
//Below I am lost!
ship = player.ships[i];
shipPresent = ship.locations.filter(function (actualCoordinate) {
return (actualCoordinate[0] === coordinates[0]) && (actualCoordinate[1] === coordinates[1]);
})[0];
if(!shipPresent) {
return false;
}
}
}
module.exports.checkForShip = checkForShip;
Any help would be appreciated.
3 Answers
Steven Parker
231,275 PointsThe "filter" function returns an array of coordinates that match. Since only one set is being checked, the array will have either one element, or none. Then by getting the [0] index, "shipPresent" will be either the matching coordinates or undefined.
But don't spend much time looking at the rest of this function, because at this point it is not completely or correctly implemented. In the following videos, this function will get changed and perhaps make more sense.
Zack Jackson
30,220 Pointsship = player.ships[i]; defines the ship from the ships array. The ships array belongs to each player since, each player has their own ships. The i in this case changes with each iteration of the for loop so that all of the ships locations are checked one by one.
shipPresent = ship.locations.filter(function (actualCoordinate) { return (actualCoordinate[0] === coordinates[0]) && (actualCoordinate[1] === coordinates[1]); })[0]; This part of the code checks whether or not any of the player's ships are located at the coordinates. The coordinates array has two integers: x and y. These are in the 0 and 1 position of the array. So this filter method checks if the passed in coordinates of the other player's guess match exactly where one of the ships is located. If they match exactly, the result is true.
if(!shipPresent) { return false; } if the coordinates don't match exactly, then the result is false.
Hope this helps to explain the logic.
Roger Hwang
3,851 PointsI took a screenshot of the players object and followed along what he was referring to which made things more clear when you can see the object he's working off of. Refer to this:
players = {
ships: [
{
locations: [[0, 0]];
};
];
};
Brian Patterson
19,588 PointsBrian Patterson
19,588 PointsThanks. Still don't get it though. If you say it becomes clearer later then I will hang on to that.