Title: Chapter 8 - Battleship · Issue #23 · bethrobson/Head-First-JavaScript-Programming · GitHub
Open Graph Title: Chapter 8 - Battleship · Issue #23 · bethrobson/Head-First-JavaScript-Programming
X Title: Chapter 8 - Battleship · Issue #23 · bethrobson/Head-First-JavaScript-Programming
Description: 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 t...
Open Graph Description: 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 hit...
X Description: 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")...
Opengraph URL: https://github.com/bethrobson/Head-First-JavaScript-Programming/issues/23
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Chapter 8 - Battleship ","articleBody":"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:\r\nI 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!\"\r\n\r\nMy code:\r\n`var view = {\r\n displayMessage: function(msg){\r\n var messageArea = document.getElementById('messageArea');\r\n messageArea.innerHTML = msg;\r\n },\r\n displayHit: function(location){\r\n var cell = document.getElementById(location);\r\n cell.setAttribute(\"class\", \"hit\");\r\n },\r\n displayMiss: function(location) {\r\n var cell = document.getElementById(location);\r\n cell.setAttribute(\"class\", \"miss\");\r\n }\r\n};\r\n\r\nvar model = {\r\n boardSize : 7,\r\n numShips: 3,\r\n shipLength: 3,\r\n shipsSunk: 0,\r\n\r\n ships:[\r\n {locations: [\"10\", \"20\", \"30\"], hits: [\"\", \"\", \"\"]},\r\n {locations: [\"32\",\"33\",\"34\"], hits:[\"\", \"\", \"\"]},\r\n {locations: [\"63\",\"64\",\"65\"], hits:[\"\", \"\", \"\"]}\r\n],\r\n\r\n fire: function(guess){\r\n for(var i = 0; i \u003c this.numShips; i++) {\r\n var ship = this.ships[i];\r\n var index = ship.locations.indexOf(guess);\r\n\r\n if (index \u003e=0) {\r\n //trafiony\r\n ship.hits[index] = \"hit\";\r\n view.displayHit(guess);\r\n view.displayMessage(\"Hit!\");\r\n\r\n if (this.isSunk(ship)){\r\n view.displayMessage(\"Sunk!\");\r\n this.shipsSunk++;\r\n }\r\n return true;\r\n }\r\n }\r\n view.displayMiss(guess);\r\n view.displayMessage(\"Miss!\")\r\n return false;\r\n },\r\n isSunk: function(ship) {\r\n for (var i=0; i \u003c this.shipLength; i++){\r\n if (ship.hits[i] !== \"hit\") {\r\n return false;\r\n }\r\n return true;\r\n }\r\n }\r\n};\r\n`\r\n\r\nI 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? \r\nBtw. this book is awesome. I bought a polish version :)","author":{"url":"https://github.com/akyjoon","@type":"Person","name":"akyjoon"},"datePublished":"2017-08-07T23:11:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":17},"url":"https://github.com/23/Head-First-JavaScript-Programming/issues/23"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c4be3d48-58b5-7738-c3dd-38a54637781a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F46:20B59C:73E96D:9A1AC3:697314B9 |
| html-safe-nonce | 3bd557469e27ca8da81aee6a937cf8586c5eb2f2ce3847c46382471769c1977c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjQ2OjIwQjU5Qzo3M0U5NkQ6OUExQUMzOjY5NzMxNEI5IiwidmlzaXRvcl9pZCI6IjU5MzAwMDY3NjI2MTY5ODQ3NjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3cd64773d4fe220d0c90d03f23c145deb111a9f336974fd15bc09f50de4a21bc |
| hovercard-subject-tag | issue:248556650 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/bethrobson/Head-First-JavaScript-Programming/23/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f93bb62677677ef621e35d993a16dcfa5fde2ab79ff9557afadfa6451fd93c33/bethrobson/Head-First-JavaScript-Programming/issues/23 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f93bb62677677ef621e35d993a16dcfa5fde2ab79ff9557afadfa6451fd93c33/bethrobson/Head-First-JavaScript-Programming/issues/23 |
| og:image:alt | 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 hit... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | akyjoon |
| hostname | github.com |
| expected-hostname | github.com |
| None | 44ab3188c1dcfe3be0f9c3feca2e04e14fb79f120939ce2395e4f15ab96ec1d4 |
| turbo-cache-control | no-preview |
| go-import | github.com/bethrobson/Head-First-JavaScript-Programming git https://github.com/bethrobson/Head-First-JavaScript-Programming.git |
| octolytics-dimension-user_id | 30760 |
| octolytics-dimension-user_login | bethrobson |
| octolytics-dimension-repository_id | 8868903 |
| octolytics-dimension-repository_nwo | bethrobson/Head-First-JavaScript-Programming |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 8868903 |
| octolytics-dimension-repository_network_root_nwo | bethrobson/Head-First-JavaScript-Programming |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | a5e2b48bd1260476599758f5d253b5d24092ab84 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width