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 Unit Testing Behavior Driven Development with Mocha & Chai Answer: Expanding Our Expectations

What is Guil assuming with the damage array?

I just wanted to understand Guil's thinking with the damage array. From how I view it, when the damage array is the same as the locations array, is that how a 'sunken ship' is determined? Then I assume that there will be some logic in place whereby once all ships have been damaged (all ships will have location and damage as the same array elements), a winner is determined?

Thanks for the help.

Regards, Deb

From this setup, I would write a function that triggers after the fire function to check if each location on a ship is contained in the damage array (meaning all the spots on the ship have been hit). Then you could set a destroyed property on the ship. Then I would have the game check to see if all a player's ships had been destroyed and message the players. There are endless ways you could decide to set up and track to see when a ship has been 'destroyed' and when a player has won. You could simply track the number of destroyed ships as a property on the player object (counting up or down) and check for it after each turn and not deal with comparing the arrays to determine a winner, etc.

I'm sure someone who frequently codes game logic will have an opinion on best practices for handling these types of functions, but you can get it to work in a million different ways.