Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Unit Testing Behavior Driven Development with Mocha & Chai Challenge: Expanding Our Expectations

Brian Patterson
Brian Patterson
19,588 Points

Found this difficult to understand

Again can someone explain this in plain English.

function damageShip (ship, coordinates) {

I understand that coordinates is being pushed onto an array. Also, what does this mean in plain English.

describe('damageShip', function (){
    var damageShip = require('../game_logic/ship_methods').damageShip;

    it('should register damage on a given ship at a given location', function(){
        var ship = {
            locations: [[0, 0]],
            damage: []
        damageShip(ship, [0,0]);
        expect(ship.damage[0]).to.deep.equal([0, 0]);


1 Answer

Zack Jackson
Zack Jackson
30,220 Points

function damageShip (ship, coordinates) { ship.damage.push(coordinates) }

The damage array is empty to begin with as we defined in our test object. Here in this function, we are pushing the coordinates into the damage array.

The test checks whether or not the array is empty. In the beginning, we defined as an empty array like this: damage: []. In our test file, we called the damageShip function like damageShip(ship, [0,0]); In doing so, we pushed [0,0] into the damage array, thus making the mocha test pass since the test was expecting the damage array to have something in it (ie not empty).