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

Brian Patterson
Brian Patterson
19,588 Points

I didn't enjoy this course! what does this mean expect(player.ships[0].damage[0]).to.deep.equal([0, 0]);

I really have not enjoyed this course. Maybe I am being really thick! I found the explanations on the coordinates very confusing.

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

    it('should record damage on the given players ship at a given coordinate', function () {
        var player = {
            ships: [
                {
                    locations: [[0,0]],
                    damage: []
                }
            ]
        };
        fire(player, [0, 0]);
    expect(player.ships[0].damage[0]).to.deep.equal([0, 0]);
    });
    it('should NOT record damage if there is no ship at my coordinates', function () {
        var player = {
            ships: [
                {
                    locations: [[0,0]],
                    damage: []
                }
            ]
        };
        fire(player, [9, 9]);
    expect(player.ships[0].damage).to.be.empty;
    });
});

It would be good if someone could explain this in plain English.

3 Answers

Steven Parker
Steven Parker
231,275 Points

Let's break this down and look at the parts:

  • expect :point_left: checking for the right result
  • player.ships[0].damage[0] :point_left: look at the first damage entry on the player's first ship
  • .to.deep.equal :point_left: look inside ("deep") the result to see if the values match
  • [0, 0] :point_left: the damage value should be an array containing 0's for both coordinates.

Does that help clear it up?

Brian Patterson
Brian Patterson
19,588 Points

Well done to Katie Schmidt for breaking down the test suite.

Brian Patterson
Brian Patterson
19,588 Points

Hi Steven thanks for getting back to me. I am confused again, no surprise there then, so can you confirm what this means

player.ships[0].damage[0]

again?

Katie Schmidt
Katie Schmidt
5,353 Points

I don't know if you already figured all of this out, or if this will help, but I figured I would give it a shot.

player is the object.

ships is an array that the player object holds and each ship in the array will contain a locations array and a damage array. (currently, there is only one ship in the ships array)

Both the locations and damage properties are arrays so in order to pick the correct ships damage you have to specify which one you want using its index number.

So in this case, player.ships[0].damage[0] , the Zeros are referring to the 0 index (the first number in the array because counting indices start at 0, not 1)

Basically, it means the first value in the damage array of the first ship in the ships array of the player object.

I hope this was a helpful answer, thats how I looked at it. (Also, this is my first comment on TreeHouse! Whoo)

Steven Parker
Steven Parker
231,275 Points

I guess I missed that last question. But welcome, new contributor!

Brian Patterson
Brian Patterson
19,588 Points

So is [0] is an array for both ships and damage ?

Steven Parker
Steven Parker
231,275 Points

The coordinate arrays have two values (for x and y), but yes there are similar coordinates for ship location and damage.