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 trialRick Klaras
20,183 PointsTypeError: Cannot read property 'locations' of undefined
Everything is fine until around the 8-minute mark when I change ship_methods.js to
if (shipPresent){
return true;
}
}
return false;
}
After that, I get these errors:
1) checkForShip should correctly report no ship at a given player's coordinate:
TypeError: Cannot read property 'locations' of undefined
at checkForShip (game_logic/ship_methods.js:7:23)
at Context.<anonymous> (test/ship_test.js:16:12)
2) checkForShip should handle ships located at more than one coordinate:
TypeError: Cannot read property 'locations' of undefined
at checkForShip (game_logic/ship_methods.js:7:23)
at Context.<anonymous> (test/ship_test.js:44:12)
3) checkForShip should handle checking multiple ships:
TypeError: Cannot read property 'locations' of undefined
at checkForShip (game_logic/ship_methods.js:7:23)
at Context.<anonymous> (test/ship_test.js:64:12)
Any ideas?
1 Answer
Sebastian Andersson
Full Stack JavaScript Techdegree Student 13,003 PointsI had the exact same problem. This is what my for-loop looked like:
for (var i = 0; player.ships.length; i++)
and this is what is was supposed to look like:
for (var i = 0; player.ships.length > i; i++)
In other words, I had not specified how many laps the for loop was going to run.
Rick Klaras
20,183 Pointsbelated thanks!
Cheo R
37,150 PointsCheo R
37,150 PointsYour code as an extra closing curly bracket after return true. Remove it and try rerunning your code.