-
Notifications
You must be signed in to change notification settings - Fork 346
Chapter 8 - Battleship #23
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
Comments
Hi, Glad you like the book! |
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? |
Oh good! Yes, sometimes the browser will cache the JavaScript. If you shift-reload, that can help. |
Hello, |
I don't even remember why I'm a part of this thread! 🤔
…On 1 Dec 2017 23:05, "Elisabeth Robson" ***@***.***> wrote:
It's here: https://github.com/bethrobson/Head-First-JavaScript-
Programming/tree/master/chapter8
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#23 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/Ab4S7hd3EGgHI815Omb_scDSTYYmhoe0ks5s8IYmgaJpZM4OwDnF>
.
|
Sorry, I meant I don't remember HOW I'm a part of this thread. 😆
On 1 Dec 2017 23:35, "Padraig O'Fearraigh" <padraig.ofearraigh@gmail.com>
wrote:
… I don't even remember why I'm a part of this thread! 🤔
On 1 Dec 2017 23:05, "Elisabeth Robson" ***@***.***> wrote:
> It's here: https://github.com/bethrobson/Head-First-JavaScript-Programm
> ing/tree/master/chapter8
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <#23 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/Ab4S7hd3EGgHI815Omb_scDSTYYmhoe0ks5s8IYmgaJpZM4OwDnF>
> .
>
|
Thanks, Elisabeth !
Am 02.12.2017 um 00:05 schrieb Elisabeth Robson:
…
It's here:
https://github.com/bethrobson/Head-First-JavaScript-Programming/tree/master/chapter8
—
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/AdOou_Y9xIO-WJBa14Nbnzra4xbs34rHks5s8IYmgaJpZM4OwDnF>.
|
You're spelling "length" wrong in:
if (guess === null || guess.lenght !== 2)
So your code won't run properly.
…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>
.
|
Thank you very much my bad |
good day still need help in understanding this code var view = {
}; view.displayMessage("Tap tap, is this thing on?"); var model = {
}; function parseGuess (guess) {
} var controller = {
}; controller.processGuess("A0"); |
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. Thanks for your comment. |
You're missing a "this" in front of "ships", it should be "this.ships". |
Sweet thank you |
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,
ships:[
{locations: ["10", "20", "30"], hits: ["", "", ""]},
{locations: ["32","33","34"], hits:["", "", ""]},
{locations: ["63","64","65"], hits:["", "", ""]}
],
fire: function(guess){
for(var i = 0; i < this.numShips; i++) {
var ship = this.ships[i];
var index = ship.locations.indexOf(guess);
},
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 :)
The text was updated successfully, but these errors were encountered: