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 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) {
    ship.damage.push(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).to.not.be.empty;
        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).