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 Writing Our First Test Suite - Part 2

TypeError: 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?

Cheo R
Cheo R
37,150 Points

Your code as an extra closing curly bracket after return true. Remove it and try rerunning your code.

if (shipPresent){
      return true;
    }
  }

  return false;
}

1 Answer

Sebastian Andersson
seal-mask
.a{fill-rule:evenodd;}techdegree
Sebastian Andersson
Full Stack JavaScript Techdegree Student 13,003 Points

I 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.

belated thanks!