Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
micronism opened this issue Aug 23, 2019 · 0 comments
Closed

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

micronism opened this issue Aug 23, 2019 · 0 comments

Comments

@micronism
Copy link

micronism commented Aug 23, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant