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
shu Chan
2,951 PointsCannot read property 'push' of undefined
Hello, I'm having some trouble understanding why this is coming out to have a typeError: Cannot read property 'push' of undefined.
here is the code
function checkForShip(player, coordinates){
let 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[1] === coordinates[1])
})[0]
if(shipPresent){
ship.damage.push(coordinates)
return true;
}
}
return false
}
here is the data that is coming from the test
describe('fireShip', function(){
let fireOnShip = require('../game_logic/ship_methods').fireOnShip;
it('should record damage on the given players ship at a given coordinate', function(){
let player = {
ships: [
{
locations: [[0,0], [0,1], [0,2]],
damage: []
}
]
}
fireOnShip(player, [0,0])
expect(player.ships[0].damage).to.not.be.empty
})
})
1 Answer
matthewnalliah
3,030 PointsAll I can see is that your data is commented out.
shu Chan
2,951 Pointsshu Chan
2,951 Pointssorry i've updated the question with the full process