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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![kingsley Emeka](https://secure.gravatar.com/avatar/7b0afac9d9561a6a4a71c751a814d650?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
kingsley Emeka
Full Stack JavaScript Techdegree Graduate 18,238 PointsFailed npm test result
When I run npm test after defining test spec for "should correctly report a ship located at the given coordinate" "should correctly handle ships located at more than one coordinate" i keep getting AssertationError: "expected false to be true"
Please, what am I doing wrong? below is my test script.
ship_test.js
var expect = require("chai").expect;
describe("checkForShip", function() {
var checkForShip = require("../game_logic/ship_methods").checkForShip;
it("should correctly report no ship at a given players coordinate", function() {
player = {
ships: [
{
locations: [[0, 0]]
}
]
};
expect(checkForShip(player, [9, 9])).to.be.false;
});
it("should correctly report a ship located at the given coordinate", function() {
player = {
ships: [
{
locations: [[0, 0]]
}
]
};
expect(checkForShip(player, [0, 0])).to.be.true;
});
it("should correctly handle ships located at more than one coordinate", function() {
player = {
ships: [
{
locations: [[0, 0], [0, 1]]
}
]
};
expect(checkForShip(player, [0, 1])).to.be.true;
expect(checkForShip(player, [0, 0])).to.be.true;
expect(checkForShip(player, [9, 9])).to.be.false;
});
});
ship_methods.js
function checkForShip(player, coordinates) {
var shipPresent, ship;
for (var i = 0; i < player.ships.length; i++) {
ship = player.ships[i];
shipPresent = ship.locations.filter(function(actualCoordinate) {
return (
actualCoordinate[0] === [0] && actualCoordinate[1] === coordinates[1]
);
})[0];
if (!shipPresent) {
return false;
} else {
return true;
}
}
}
module.exports.checkForShip = checkForShip;
1 Answer
![David Ray](https://uploads.teamtreehouse.com/production/profile-photos/11529670/micro_eastwood_by_elchez.jpg)
David Ray
6,691 PointsI have exactly the same issue:
function checkForShip(player, coordinates){
var shipPresent, ship;
for(var i = 0; i < player.ships.length; i++){
ship = player.ships[i];
shipPresent = ship.locations.filter(function (actualCoordinate){
return (actualCoordinate[0]=== coordinates[0]) && (actualCoordinate[0]=== coordinates[1])})[0];
if (shipPresent){
return true;
}
}
return false;
}
module.exports.checkForShip = checkForShip;
var expect = require('chai').expect;
describe('checkForShip', function () {
var checkForShip = require('../game_logic/ship_methods').checkForShip;
it('should correctly report no ship at a given players coordinate', function () {
player = {
ships: [{
locations: [
[0, 0]
]
}]
};
expect(checkForShip(player, [9, 9])).to.be.false;
});
it('should correctly report a ship at the given players coordinates', function () {
player = {
ships: [{
locations: [
[0, 0]
]
}]
};
expect(checkForShip(player, [0, 0])).to.be.true;
});
it('should handle ships at more than one coordinate', function () {
player = {
ships: [{
locations: [
[0, 0],
[0, 1]
]
}]
};
expect(checkForShip(player, [0, 1])).to.be.true;
expect(checkForShip(player, [0, 0])).to.be.true;
expect(checkForShip(player, [9, 9])).to.be.false;
});
it('should handle multiple ships', function () {
player = {
ships: [{
locations: [
[0, 0],
[0, 1]
]
},
{
locations: [
[1, 0],
[1, 1]
]
},
{
locations: [
[2, 0],
[2, 1][2, 2],
[2, 3]
]
}
]
};
expect(checkForShip(player, [0, 0])).to.be.true;
expect(checkForShip(player, [0, 1])).to.be.true;
expect(checkForShip(player, [1, 0])).to.be.true;
expect(checkForShip(player, [1, 1])).to.be.true;
expect(checkForShip(player, [2, 0])).to.be.true;
expect(checkForShip(player, [2, 1])).to.be.true;
expect(checkForShip(player, [2, 2])).to.be.true;
expect(checkForShip(player, [2, 3])).to.be.true;
expect(checkForShip(player, [9, 9])).to.be.false;
});
});
I continued the lesson to see if it would cover this.. but it didn't.