You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm on chapter 8 where you build a battleship game. I have finished the Viewer and Model part without the Controller part. My problem:
I type in the console model.fire("somecell") to make 3 hits and drown a ship, but I keep getting that the ship is sunk on every hit. It never says "Hit!"
My code:
`var view = {
displayMessage: function(msg){
var messageArea = document.getElementById('messageArea');
messageArea.innerHTML = msg;
},
displayHit: function(location){
var cell = document.getElementById(location);
cell.setAttribute("class", "hit");
},
displayMiss: function(location) {
var cell = document.getElementById(location);
cell.setAttribute("class", "miss");
}
};
var model = {
boardSize : 7,
numShips: 3,
shipLength: 3,
shipsSunk: 0,
},
isSunk: function(ship) {
for (var i=0; i < this.shipLength; i++){
if (ship.hits[i] !== "hit") {
return false;
}
return true;
}
}
};
`
I compared the code and it's the same as in the book and don't understand why it won't work. Can you help me?
Btw. this book is awesome. I bought a polish version :)
Thanks for the reply. Yes I ran it again today and it works! Yesterday it didn't run and console didn't show anything... Is there any possibility that the browser remembered the old wrong code and didn't run properly the one that actually should run?
Hello,
I can not find the download link for chapter 8 for Buttleship with images.
There is no Dowload menu in the http://www.wickedlysmart.com/hfjs/
I would be very appreciate for yor help.
Hello, Good day am on Chapter 8 where you implement the parseGuess function I'm having issue as am not getting the required results in my console as in the book.
here is the code
function parseGuess (guess) {
var alphabet = ["A", "B", "C", "D", "E", "F", "G"];
if (guess === null || guess.lenght !== 2) {
alert("Oops, please enter a letter and a number on the board.");
} else {
firstChar = guess.charAt(0);
var row = alphabet.indexOf(firstChar);
var column = guess.charAt(1);
if (isNaN(row) || isNaN(column)) {
alert("Oops, that isnt on the board.");
} else if (row < 0 || row >= model.boardSize ||
column < 0 || column >= model.boardSize) {
alert("Oops, that's off the board!");
} else {
return row + column;
}
}
return null;
On Thu, Dec 6, 2018 at 9:14 AM juwie5 ***@***.***> wrote:
Hello, Good day am on Chapter 8 where you implement the parseGuess
function I'm having issue as am not getting the required results in my
console as in the book.
[image: hf javascript ch8]
<https://user-images.githubusercontent.com/41777756/49599542-945cb080-f981-11e8-954e-a1c1892c3efe.JPG>
here is the code
function parseGuess (guess) {
var alphabet = ["A", "B", "C", "D", "E", "F", "G"];
if (guess === null || guess.lenght !== 2) {
alert("Oops, please enter a letter and a number on the board.");
} else {
firstChar = guess.charAt(0);
var row = alphabet.indexOf(firstChar);
var column = guess.charAt(1);
if (isNaN(row) || isNaN(column)) {
alert("Oops, that isnt on the board.");
} else if (row < 0 || row >= model.boardSize ||
column < 0 || column >= model.boardSize) {
alert("Oops, that's off the board!");
} else {
return row + column;
}
}
return null;
}
console.log(parseGuess("A0"));
console.log(parseGuess("B6"));
console.log(parseGuess("G3"));
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#23 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAB4KBuyka27twErwGFcTQP70lHV0ZRFks5u2VCPgaJpZM4OwDnF>
.
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";
view.displayHit(guess);
view.displayMessage("HIT!");
if (this.isSunk(ship)) {
view.displayMessage("You sank my battleship!");
this.shipsSunk++;
}
return true;
}
}
view.displayMiss(guess);
view.displayMessage("You missed.");
return false;
},
isSunk: function(ship) {
for (var i = 0; i < this.shipLenght; i++) {
if (ship.hits[i] !== "hit"){
return false;
}
}
return true;
}
};
function parseGuess (guess) {
var alphabet = ["A", "B", "C", "D", "E", "F", "G"];
if (guess === null || guess.length !== 2) {
alert("Oops, please enter a letter and a number on the board.");
} else {
firstChar = guess.charAt(0);
var row = alphabet.indexOf(firstChar);
var column = guess.charAt(1);
if (isNaN(row) || isNaN(column)) {
alert("Oops, that isnt on the board.");
} else if (row < 0 || row >= model.boardSize ||
column < 0 || column >= model.boardSize) {
alert("Oops, that's off the board!");
} else {
return row + column;
}
}
return null;
}
var controller = {
guesses : 0,
processGuess: function(guess) {
var location = parseGuess(guess);
if(location) {
this.guesses++;
var hit = model.fire(location);
if (hit && model.shipsSunk === model.numShips){
view.displayMessage ("You sank all my battleships, in " + this.guesses + "guesses");
}
}
}
Hello, I just started this exercise and so saw tables being used. I'm surprised...aren't tables old practice. Surely better to use divs with css flex layout??
Jonathan, there's nothing wrong with using tables - they are a valid way to display data. Flex works great especially if you need flexibility in cell sizing or row sizing, but we didn't need that for this grid. The concepts of modifying HTML and CSS with JavaScript are the similar no matter what underlying structure you use, and in this case, using a table was easier and more straightforward. In addition, the book is about JavaScript, not HTML & CSS, and we wanted to focus on the JS concepts rather than explaining flex which is trickier to understand for people not familiar with it.
Activity
bethrobson commentedon Aug 8, 2017
Hi,
I just ran the code from the book and it works fine, so you must have a bug somewhere! Use the console to help track it down.
Glad you like the book!
akyjoon commentedon Aug 8, 2017
Thanks for the reply. Yes I ran it again today and it works! Yesterday it didn't run and console didn't show anything... Is there any possibility that the browser remembered the old wrong code and didn't run properly the one that actually should run?
bethrobson commentedon Aug 8, 2017
Oh good! Yes, sometimes the browser will cache the JavaScript. If you shift-reload, that can help.
sssony commentedon Dec 1, 2017
Hello,
I can not find the download link for chapter 8 for Buttleship with images.
There is no Dowload menu in the http://www.wickedlysmart.com/hfjs/
I would be very appreciate for yor help.
bethrobson commentedon Dec 1, 2017
It's here: https://github.com/bethrobson/Head-First-JavaScript-Programming/tree/master/chapter8
padraigofearraigh commentedon Dec 1, 2017
padraigofearraigh commentedon Dec 1, 2017
sssony commentedon Dec 2, 2017
juwie5 commentedon Dec 6, 2018
Hello, Good day am on Chapter 8 where you implement the parseGuess function I'm having issue as am not getting the required results in my console as in the book.
here is the code
function parseGuess (guess) {
var alphabet = ["A", "B", "C", "D", "E", "F", "G"];
}
console.log(parseGuess("A0"));
console.log(parseGuess("B6"));
console.log(parseGuess("G3"));
bethrobson commentedon Dec 6, 2018
juwie5 commentedon Dec 7, 2018
Thank you very much my bad
juwie5 commentedon Dec 18, 2018
good day still need help in understanding this code
var view = {
displayMessage: function(msg) {
var messageArea = document.getElementById("messageArea");
messageArea.innerHTML = msg;
},
};
view.displayMessage("Tap tap, is this thing on?");
var model = {
boardSize: 7,
numShips: 3,
shipsSunk: 0,
shipLenght: 3,
ships: [ {locations:["06", "16", "26"], hits:["", "", ""] },
{locations:["24", "34", "44"], hits:["", "", ""] },
{locations:["10", "11", "12"], hits:["", "", ""] }
],
};
function parseGuess (guess) {
var alphabet = ["A", "B", "C", "D", "E", "F", "G"];
}
var controller = {
guesses : 0,
};
controller.processGuess("A0");
jonathanfowler commentedon Jul 27, 2019
Hello, I just started this exercise and so saw tables being used. I'm surprised...aren't tables old practice. Surely better to use divs with css flex layout??
bethrobson commentedon Jul 29, 2019
Jonathan, there's nothing wrong with using tables - they are a valid way to display data. Flex works great especially if you need flexibility in cell sizing or row sizing, but we didn't need that for this grid. The concepts of modifying HTML and CSS with JavaScript are the similar no matter what underlying structure you use, and in this case, using a table was easier and more straightforward. In addition, the book is about JavaScript, not HTML & CSS, and we wanted to focus on the JS concepts rather than explaining flex which is trickier to understand for people not familiar with it.
Thanks for your comment.
Geisel-Diesel commentedon Aug 26, 2020
Hi, I'm getting this error?

bethrobson commentedon Aug 26, 2020
You're missing a "this" in front of "ships", it should be "this.ships".
Geisel-Diesel commentedon Aug 26, 2020
Sweet thank you