bethrobson / Head-First-JavaScript-Programming

418 stars 344 forks source link

How/where do we call the isSunk method in chapter 8 ? #24

Closed micronism closed 5 years ago

micronism commented 5 years ago

`var model = { boardSize: 7, numShips: 3, shipLength: 3, shipsSunk: 0,

ships: [{ locations: ["06", "16", "26"], hits: ["", "", ""] }, { locations: ["24", "34", "44"], hits: ["", "", ""] }, { locations: ["10", "11", "12"], hits: ["", "", ""] }

], fire: function(guess) { for (var i = 0; i < this.numShips; i++) { var ship = this.ships[i]; var index = ship.locations.indexOf(guess); if (index >= 0) { ship.hits[index] = "hit"; if (this.isSunk(ship)) {
this.shipsSunk++; }
return true; } } return false; }, isSunk: function(ship) { for (var i = 0; i < this.shipLength; i++) { if (ship.hits[i] !== "hit") { return false; } } return true; } }; ` Edit: The first if-statement above calls the method isSunk and then the body of the isSunk method will be executed. And if it's true, the code jumps back to if (this.isSunk(ship)) and executes the body.