├── chapter9 ├── map.jpg ├── one.jpg ├── two.jpg ├── five.jpg ├── four.jpg ├── three.jpg ├── zero.jpg ├── fiveblur.jpg ├── fourblur.jpg ├── oneblur.jpg ├── twoblur.jpg ├── zeroblur.jpg ├── threeblur.jpg ├── event.html ├── image_start.html ├── resize.html ├── image.html ├── map.html ├── ticktock.html ├── imageMultiple.html ├── imageReblur.html └── imageMouse.html ├── chapter8 ├── board.jpg ├── miss.png ├── ship.png ├── battleship_tester.html ├── battleship.html ├── battleship.js └── battleship_tester.js ├── chapter6 ├── code.js ├── planets.html └── drevel.html ├── chapter11 ├── chapter9WithClosure │ ├── five.jpg │ ├── four.jpg │ ├── one.jpg │ ├── three.jpg │ ├── two.jpg │ ├── zero.jpg │ ├── oneblur.jpg │ ├── twoblur.jpg │ ├── fiveblur.jpg │ ├── fourblur.jpg │ ├── threeblur.jpg │ ├── zeroblur.jpg │ └── imageClosure.html ├── timerClosure.html ├── counterClosure.html ├── justAVar.html ├── justSayin.html ├── flyAndQuack.html ├── vaccine.html ├── challenge.html ├── bakeSolution.html ├── closureExercise.html └── divClosure.html ├── README.md ├── chapter1 ├── howdy.html ├── code.js ├── kids.html ├── birthday.html ├── behavior.html ├── generic.html ├── icecream.html └── index.html ├── chapter2 ├── battleship.html ├── battleship.js └── battleship-final.js ├── chapter7 ├── duck.html ├── typeof.html └── types.html ├── appendix ├── exceptions.html ├── regexp.html ├── fibonacci.html ├── JSON.html ├── jquery.html ├── arguments.html ├── events.html ├── dom.html └── listener.html ├── chapter10 ├── aloha.html ├── addN.html ├── fun.html ├── shellgame.html ├── cola.html └── plane.html ├── chapter3 ├── bark.html ├── sherlock.html ├── bake.html ├── areaAndDistance.html └── thingamajig.html ├── chapter4 ├── icecream.html ├── phrase-o-matic.html ├── bubbles.html └── bubbles2.html ├── chapter13 ├── robotOverride.html ├── dog.html ├── cliche.html ├── robotsHasOwnProperty.html ├── dog1.html ├── robotsGame.html ├── palindrome.html ├── dog2.html ├── dog3.html ├── robots.html ├── dog4.html ├── dog5.html ├── spacerobots.html ├── showdogs.html ├── showdogsCleanup.html └── showdogsTester.html ├── chapter5 ├── eightball.html ├── carProps.html ├── carWithDrive.html ├── secret.html ├── autoomatic.html ├── carWithFuel.html ├── prequal.html └── carWithDriveExercise.html ├── chapter12 ├── dog1.html ├── coffee.html ├── dog2.html ├── dogCatcher.html ├── cadi.html ├── car.html └── carAndDog.html └── extras ├── timer.html ├── varWithClosures.html └── letWithClosures.html /chapter9/map.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/map.jpg -------------------------------------------------------------------------------- /chapter9/one.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/one.jpg -------------------------------------------------------------------------------- /chapter9/two.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/two.jpg -------------------------------------------------------------------------------- /chapter8/board.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter8/board.jpg -------------------------------------------------------------------------------- /chapter8/miss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter8/miss.png -------------------------------------------------------------------------------- /chapter8/ship.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter8/ship.png -------------------------------------------------------------------------------- /chapter9/five.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/five.jpg -------------------------------------------------------------------------------- /chapter9/four.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/four.jpg -------------------------------------------------------------------------------- /chapter9/three.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/three.jpg -------------------------------------------------------------------------------- /chapter9/zero.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/zero.jpg -------------------------------------------------------------------------------- /chapter9/fiveblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/fiveblur.jpg -------------------------------------------------------------------------------- /chapter9/fourblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/fourblur.jpg -------------------------------------------------------------------------------- /chapter9/oneblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/oneblur.jpg -------------------------------------------------------------------------------- /chapter9/twoblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/twoblur.jpg -------------------------------------------------------------------------------- /chapter9/zeroblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/zeroblur.jpg -------------------------------------------------------------------------------- /chapter9/threeblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter9/threeblur.jpg -------------------------------------------------------------------------------- /chapter6/code.js: -------------------------------------------------------------------------------- 1 | var access = document.getElementById("code9"); 2 | var code = access.innerHTML; 3 | code = code + " midnight"; 4 | alert(code); 5 | -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/five.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/five.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/four.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/four.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/one.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/one.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/three.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/three.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/two.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/two.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/zero.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/zero.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/oneblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/oneblur.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/twoblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/twoblur.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/fiveblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/fiveblur.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/fourblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/fourblur.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/threeblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/threeblur.jpg -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/zeroblur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bethrobson/Head-First-JavaScript-Programming/HEAD/chapter11/chapter9WithClosure/zeroblur.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Code for Head First JavaScript Programming, by Elisabeth Robson and Eric Freeman. 2 | Published in March, 2014. 3 | 4 | http://wickedlysmart.com/hfjs 5 | 6 | 7 | -------------------------------------------------------------------------------- /chapter1/howdy.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Howdy 6 | 7 | 8 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /chapter2/battleship.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Battleship 6 | 7 | 8 |

Play battleship!

9 |
10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /chapter9/event.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | I'm alive! 5 | 6 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /chapter1/code.js: -------------------------------------------------------------------------------- 1 | scoops = 5; 2 | while (scoops > 0) { 3 | document.write("Another scoop!
"); 4 | if (scoops < 3) { 5 | alert("Ice cream is running low!"); 6 | } else if (scoops >= 5) { 7 | alert("Eat faster, the ice cream is going to melt!"); 8 | } 9 | scoops = scoops - 1; 10 | } 11 | document.write("Life without ice cream isn't the same"); 12 | -------------------------------------------------------------------------------- /chapter9/image_start.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Image Guess 7 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /chapter1/kids.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | For Kids Only 6 | 15 | 16 | 17 |

This page is for kids only!

18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /chapter11/timerClosure.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Timer closure 6 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /chapter7/duck.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Ducks 6 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /chapter1/birthday.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Happy Birthday 6 | 7 | 8 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /appendix/exceptions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Exception handling 6 | 18 | 19 | 20 |
21 | 22 | 23 | -------------------------------------------------------------------------------- /appendix/regexp.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Regular Expressions 6 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /chapter10/aloha.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Aloha 6 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /appendix/fibonacci.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Computing Fibonacci numbers recursively 6 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /chapter3/bark.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Bark 6 | 7 | 8 | 9 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /chapter11/counterClosure.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Counter closure 6 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /chapter9/resize.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Don't resize me, I'm ticklish! 6 | 12 | 13 | 14 |

15 | Whatever you do, don’t resize this window! I'm warning you! 16 |

17 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /chapter1/behavior.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Just a Generic Page 6 | 12 | 13 | 14 |

Just a generic heading

15 |

Not a lot to read about here. I'm just an obligatory paragraph living in 16 | an example in a JavaScript book. I'm looking for something to make my life more 17 | exciting.

18 | 19 | 20 | -------------------------------------------------------------------------------- /chapter10/addN.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Return a function from a function 6 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /chapter1/generic.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Just a Generic Page 6 | 12 | 13 | 14 |

Just a generic heading

15 |

Not a lot to read about here. I'm just an obligatory paragraph living 16 | in an example in a JavaScript book. I'm looking for something to make my 17 | life more exciting.

18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /chapter11/justAVar.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Extreme JavaScript Challenge 6 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /appendix/JSON.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | JSON 6 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /chapter3/sherlock.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Five Minute Mystery 6 | 7 | 8 | 9 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /chapter11/justSayin.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Free Variables Exercise 6 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /chapter10/fun.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Fun 6 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter4/icecream.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Icecream with bubblegum 6 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /appendix/jquery.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Brief intro to jQuery 6 | 13 | 14 | 25 | 26 | 27 |
I'm a special offer
28 | 29 | 30 | -------------------------------------------------------------------------------- /chapter10/shellgame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Shell Game 6 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /chapter9/image.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Image Guess 6 | 10 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /chapter13/robotOverride.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Overriding built-in behavior 6 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /chapter1/icecream.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Icecream! 6 | 7 | 8 | 9 | 10 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /chapter9/map.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Pirates Booty 6 | 20 | 21 | 22 | 23 |

Move mouse over map to find coordinates...

24 | 25 | 26 | -------------------------------------------------------------------------------- /chapter6/planets.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Planets 6 | 9 | 10 | 11 |

Green Planet

12 |

All is well

13 |

Red Planet

14 |

Nothing to report

15 |

Blue Planet

16 |

All systems A-OK

17 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /appendix/arguments.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | The arguments object 6 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /chapter9/ticktock.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Tick Tock 5 | 6 | 30 | 31 | 32 |

33 | 34 | 35 | -------------------------------------------------------------------------------- /chapter1/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | My First JavaScript 6 | 7 | 8 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /chapter11/flyAndQuack.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Scope 6 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /chapter5/eightball.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Magic Eightball 6 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /chapter13/dog.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Fido 6 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /chapter3/bake.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Bake (with return) 6 | 7 | 8 | 9 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /chapter6/drevel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Dr. Evel's Secret Code Page 6 | 7 | 8 |

The eagle is in the

9 |

The fox is in the

10 |

snuck into the garden last night.

11 |

They said it would rain

12 |

Does the red robin crow at

13 |

Where can I find Mr.

14 |

I told the boys to bring tea and

15 |

Where's my dough? The cake won't

16 |

My watch stopped at

17 |

barking, can't fly without umbrella.

The green canary flies at

18 |

The oyster owns a fine

19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /chapter7/typeof.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Testing typeof 6 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /chapter12/dog1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Construct some dogs 6 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /chapter2/battleship.js: -------------------------------------------------------------------------------- 1 | var location1 = 3; 2 | var location2 = 4; 3 | var location3 = 5; 4 | var guess; 5 | var hits = 0; 6 | var guesses = 0; 7 | var isSunk = false; 8 | 9 | while (isSunk == false) { 10 | guess = prompt("Ready, aim, fire! (enter a number from 0-6):"); 11 | if (guess < 0 || guess > 6) { 12 | alert("Please enter a valid cell number!"); 13 | } else { 14 | guesses = guesses + 1; 15 | if (guess == location1 || guess == location2 || guess == location3) { 16 | alert("HIT!"); 17 | hits = hits + 1; 18 | if (hits == 3) { 19 | isSunk = true; 20 | alert("You sank my battleship!"); 21 | } 22 | } else { 23 | alert("MISS"); 24 | } 25 | } 26 | } 27 | var stats = "You took " + guesses + " guesses to sink the battleship, " + 28 | "which means your shooting accuracy was " + (3/guesses); 29 | alert(stats); 30 | -------------------------------------------------------------------------------- /chapter4/phrase-o-matic.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Phrase-o-matic 6 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /appendix/events.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Handling events in IE8 and earlier 6 | 11 | 33 | 34 | 35 | Click me! 36 | 37 | 38 | -------------------------------------------------------------------------------- /chapter12/coffee.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Coffee Constructor 6 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /chapter11/vaccine.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | First class functions exercise 6 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /chapter9/imageMultiple.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Image Guess 6 | 10 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /appendix/dom.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Doing more with the DOM 6 | 26 | 27 | 28 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /chapter11/challenge.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Extreme JavaScript Challenge 6 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /chapter2/battleship-final.js: -------------------------------------------------------------------------------- 1 | var randomLoc = Math.floor(Math.random() * 5); 2 | var location1 = randomLoc; 3 | var location2 = location1 + 1; 4 | var location3 = location1 + 2; 5 | var guess; 6 | var hits = 0; 7 | var guesses = 0; 8 | var isSunk = false; 9 | 10 | while (isSunk == false) { 11 | guess = prompt("Ready, aim, fire! (enter a number from 0-6):"); 12 | if (guess < 0 || guess > 6) { 13 | alert("Please enter a valid cell number!"); 14 | } else { 15 | guesses = guesses + 1; 16 | if (guess == location1 || guess == location2 || guess == location3) { 17 | alert("HIT!"); 18 | hits = hits + 1; 19 | if (hits == 3) { 20 | isSunk = true; 21 | alert("You sank my battleship!"); 22 | } 23 | } else { 24 | alert("MISS"); 25 | } 26 | } 27 | } 28 | var stats = "You took " + guesses + " guesses to sink the battleship, " + 29 | "which means your shooting accuracy was " + (3/guesses); 30 | alert(stats); 31 | -------------------------------------------------------------------------------- /chapter5/carProps.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Car properties 6 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /extras/timer.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Webville Lounge 4 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /chapter5/carWithDrive.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Car with drive 6 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /extras/varWithClosures.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Let, scope, and closures 5 | 6 | 16 | 33 | 34 | 35 |

let, scope, and closures

36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /chapter13/cliche.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Extending the String prototype with a new method 6 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /chapter3/areaAndDistance.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Area and Distance 6 | 7 | 8 | 9 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /chapter11/bakeSolution.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Anonymous Functions: Bake the cookies 6 | 34 | 35 | 36 |
37 | 38 |
39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter13/robotsHasOwnProperty.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Robots 6 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /chapter5/secret.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Secret 6 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /appendix/listener.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Handling events in with addEventListener 6 | 11 | 36 | 37 | 38 | Click me! 39 | 40 | 41 | -------------------------------------------------------------------------------- /chapter13/dog1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Fido with Prototype 6 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /chapter9/imageReblur.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Image Guess 6 | 10 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /chapter12/dog2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Construct some dogs 6 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /chapter13/robotsGame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Robots Game 6 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /chapter11/chapter9WithClosure/imageClosure.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Image Guess 6 | 10 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /chapter13/palindrome.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Palindrome 6 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /chapter9/imageMouse.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Image Guess 6 | 10 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /chapter11/closureExercise.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Closure exercise 6 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /chapter13/dog2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Spot gets his WOOF! 6 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /chapter11/divClosure.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Click me! 6 | 10 | 46 | 47 | 48 | 49 |
50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /chapter7/types.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Types 6 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /extras/letWithClosures.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Let, scope, and closures 5 | 6 | 16 | 18 | 20 | 23 | 40 | 41 | 42 |

let, scope, and closures

43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /chapter3/thingamajig.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | The Thing-a-ma-jig 6 | 7 | 8 | 9 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /chapter12/dogCatcher.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Dog Catcher 6 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /chapter13/dog3.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Barnaby gets to sit 6 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /chapter5/autoomatic.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Object-o-matic 6 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /chapter5/carWithFuel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Car with fuel 6 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /chapter12/cadi.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Car Constructor 6 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /chapter13/robots.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Robots 6 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /chapter12/car.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Car Constructor 6 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /chapter13/dog4.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Adding sitting test 6 | 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /chapter13/dog5.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Who has the sitting property? 6 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /chapter5/prequal.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Prequalifcation 5 | 6 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /chapter4/bubbles.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Bubble Factory Test Lab 6 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /chapter13/spacerobots.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Space Robots 6 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /chapter13/showdogs.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Show dogs 6 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /chapter8/battleship_tester.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Battleship 5 | 6 | 52 | 53 | 54 |
55 |
56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 |
86 |
87 | 88 | 89 |
90 |
91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /chapter13/showdogsCleanup.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Show dogs 6 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /chapter13/showdogsTester.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Show dogs 6 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /chapter8/battleship.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Battleship 6 | 55 | 56 | 57 |
58 |
59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 |
89 |
90 | 91 | 92 |
93 |
94 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /chapter10/cola.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Webville Cola 5 | 6 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /chapter5/carWithDriveExercise.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | All cars driving 6 | 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /chapter4/bubbles2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Bubble Factory Test Lab 6 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /chapter10/plane.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | First class functions 6 | 133 | 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /chapter12/carAndDog.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Car Constructor 6 | 122 | 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /chapter8/battleship.js: -------------------------------------------------------------------------------- 1 | var model = { 2 | boardSize: 7, 3 | numShips: 3, 4 | shipLength: 3, 5 | shipsSunk: 0, 6 | 7 | ships: [ 8 | { locations: [0, 0, 0], hits: ["", "", ""] }, 9 | { locations: [0, 0, 0], hits: ["", "", ""] }, 10 | { locations: [0, 0, 0], hits: ["", "", ""] } 11 | ], 12 | 13 | // original hard-coded values for ship locations 14 | /* 15 | ships: [ 16 | { locations: ["06", "16", "26"], hits: ["", "", ""] }, 17 | { locations: ["24", "34", "44"], hits: ["", "", ""] }, 18 | { locations: ["10", "11", "12"], hits: ["", "", ""] } 19 | ], 20 | */ 21 | 22 | fire: function(guess) { 23 | for (var i = 0; i < this.numShips; i++) { 24 | var ship = this.ships[i]; 25 | var index = ship.locations.indexOf(guess); 26 | 27 | // here's an improvement! Check to see if the ship 28 | // has already been hit, message the user, and return true. 29 | if (ship.hits[index] === "hit") { 30 | view.displayMessage("Oops, you already hit that location!"); 31 | return true; 32 | } else if (index >= 0) { 33 | ship.hits[index] = "hit"; 34 | view.displayHit(guess); 35 | view.displayMessage("HIT!"); 36 | 37 | if (this.isSunk(ship)) { 38 | view.displayMessage("You sank my battleship!"); 39 | this.shipsSunk++; 40 | } 41 | return true; 42 | } 43 | } 44 | view.displayMiss(guess); 45 | view.displayMessage("You missed."); 46 | return false; 47 | }, 48 | 49 | isSunk: function(ship) { 50 | for (var i = 0; i < this.shipLength; i++) { 51 | if (ship.hits[i] !== "hit") { 52 | return false; 53 | } 54 | } 55 | return true; 56 | }, 57 | 58 | generateShipLocations: function() { 59 | var locations; 60 | for (var i = 0; i < this.numShips; i++) { 61 | do { 62 | locations = this.generateShip(); 63 | } while (this.collision(locations)); 64 | this.ships[i].locations = locations; 65 | } 66 | console.log("Ships array: "); 67 | console.log(this.ships); 68 | }, 69 | 70 | generateShip: function() { 71 | var direction = Math.floor(Math.random() * 2); 72 | var row, col; 73 | 74 | if (direction === 1) { // horizontal 75 | row = Math.floor(Math.random() * this.boardSize); 76 | col = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1)); 77 | } else { // vertical 78 | row = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1)); 79 | col = Math.floor(Math.random() * this.boardSize); 80 | } 81 | 82 | var newShipLocations = []; 83 | for (var i = 0; i < this.shipLength; i++) { 84 | if (direction === 1) { 85 | newShipLocations.push(row + "" + (col + i)); 86 | } else { 87 | newShipLocations.push((row + i) + "" + col); 88 | } 89 | } 90 | return newShipLocations; 91 | }, 92 | 93 | collision: function(locations) { 94 | for (var i = 0; i < this.numShips; i++) { 95 | var ship = this.ships[i]; 96 | for (var j = 0; j < locations.length; j++) { 97 | if (ship.locations.indexOf(locations[j]) >= 0) { 98 | return true; 99 | } 100 | } 101 | } 102 | return false; 103 | } 104 | 105 | }; 106 | 107 | 108 | var view = { 109 | displayMessage: function(msg) { 110 | var messageArea = document.getElementById("messageArea"); 111 | messageArea.innerHTML = msg; 112 | }, 113 | 114 | displayHit: function(location) { 115 | var cell = document.getElementById(location); 116 | cell.setAttribute("class", "hit"); 117 | }, 118 | 119 | displayMiss: function(location) { 120 | var cell = document.getElementById(location); 121 | cell.setAttribute("class", "miss"); 122 | } 123 | 124 | }; 125 | 126 | var controller = { 127 | guesses: 0, 128 | 129 | processGuess: function(guess) { 130 | var location = parseGuess(guess); 131 | if (location) { 132 | this.guesses++; 133 | var hit = model.fire(location); 134 | if (hit && model.shipsSunk === model.numShips) { 135 | view.displayMessage("You sank all my battleships, in " + this.guesses + " guesses"); 136 | } 137 | } 138 | } 139 | } 140 | 141 | 142 | // helper function to parse a guess from the user 143 | 144 | function parseGuess(guess) { 145 | var alphabet = ["A", "B", "C", "D", "E", "F", "G"]; 146 | 147 | if (guess === null || guess.length !== 2) { 148 | alert("Oops, please enter a letter and a number on the board."); 149 | } else { 150 | var firstChar = guess.charAt(0); 151 | var row = alphabet.indexOf(firstChar); 152 | var column = guess.charAt(1); 153 | 154 | if (isNaN(row) || isNaN(column)) { 155 | alert("Oops, that isn't on the board."); 156 | } else if (row < 0 || row >= model.boardSize || 157 | column < 0 || column >= model.boardSize) { 158 | alert("Oops, that's off the board!"); 159 | } else { 160 | return row + column; 161 | } 162 | } 163 | return null; 164 | } 165 | 166 | 167 | // event handlers 168 | 169 | function handleFireButton() { 170 | var guessInput = document.getElementById("guessInput"); 171 | var guess = guessInput.value.toUpperCase(); 172 | 173 | controller.processGuess(guess); 174 | 175 | guessInput.value = ""; 176 | } 177 | 178 | function handleKeyPress(e) { 179 | var fireButton = document.getElementById("fireButton"); 180 | 181 | // in IE9 and earlier, the event object doesn't get passed 182 | // to the event handler correctly, so we use window.event instead. 183 | e = e || window.event; 184 | 185 | if (e.keyCode === 13) { 186 | fireButton.click(); 187 | return false; 188 | } 189 | } 190 | 191 | 192 | // init - called when the page has completed loading 193 | 194 | window.onload = init; 195 | 196 | function init() { 197 | // Fire! button onclick handler 198 | var fireButton = document.getElementById("fireButton"); 199 | fireButton.onclick = handleFireButton; 200 | 201 | // handle "return" key press 202 | var guessInput = document.getElementById("guessInput"); 203 | guessInput.onkeypress = handleKeyPress; 204 | 205 | // place the ships on the game board 206 | model.generateShipLocations(); 207 | } 208 | 209 | 210 | 211 | 212 | 213 | -------------------------------------------------------------------------------- /chapter8/battleship_tester.js: -------------------------------------------------------------------------------- 1 | // To use this tester file, you'll need to leave the view, model, controller 2 | // objects in place, but comment out all the testing code except the parts 3 | // you're using. Remember you can use 4 | /* 5 | code here 6 | */ 7 | // to comment out large chunks of code. 8 | 9 | 10 | // testing the view 11 | var view = { 12 | displayMessage: function(msg) { 13 | var messageArea = document.getElementById("messageArea"); 14 | messageArea.innerHTML = msg; 15 | }, 16 | 17 | displayHit: function(location) { 18 | var cell = document.getElementById(location); 19 | cell.setAttribute("class", "hit"); 20 | }, 21 | 22 | displayMiss: function(location) { 23 | var cell = document.getElementById(location); 24 | cell.setAttribute("class", "miss"); 25 | } 26 | 27 | }; 28 | 29 | view.displayMiss("00"); 30 | view.displayHit("34"); 31 | view.displayMiss("55"); 32 | view.displayHit("12"); 33 | view.displayMiss("25"); 34 | view.displayHit("26"); 35 | 36 | view.displayMessage("Tap tap, is this thing on?"); 37 | 38 | // testing the model 39 | 40 | 41 | var model = { 42 | boardSize: 7, 43 | numShips: 3, 44 | shipLength: 3, 45 | shipsSunk: 0, 46 | 47 | ships: [ 48 | { locations: ["06", "16", "26"], hits: ["", "", ""] }, 49 | { locations: ["24", "34", "44"], hits: ["", "", ""] }, 50 | { locations: ["10", "11", "12"], hits: ["", "", ""] } 51 | ], 52 | 53 | fire: function(guess) { 54 | for (var i = 0; i < this.numShips; i++) { 55 | var ship = this.ships[i]; 56 | var index = ship.locations.indexOf(guess); 57 | 58 | if (index >= 0) { 59 | ship.hits[index] = "hit"; 60 | view.displayHit(guess); 61 | view.displayMessage("HIT!"); 62 | 63 | if (this.isSunk(ship)) { 64 | view.displayMessage("You sank my battleship!"); 65 | this.shipsSunk++; 66 | } 67 | return true; 68 | } 69 | } 70 | view.displayMiss(guess); 71 | view.displayMessage("You missed."); 72 | return false; 73 | }, 74 | 75 | isSunk: function(ship) { 76 | for (var i = 0; i < this.shipLength; i++) { 77 | if (ship.hits[i] !== "hit") { 78 | return false; 79 | } 80 | } 81 | return true; 82 | }, 83 | 84 | generateShipLocations: function() { 85 | var locations; 86 | for (var i = 0; i < this.numShips; i++) { 87 | do { 88 | locations = this.generateShip(); 89 | } while (this.collision(locations)); 90 | this.ships[i].locations = locations; 91 | } 92 | console.log("Ships array: "); 93 | console.log(this.ships); 94 | }, 95 | 96 | generateShip: function() { 97 | var direction = Math.floor(Math.random() * 2); 98 | var row, col; 99 | 100 | if (direction === 1) { // horizontal 101 | row = Math.floor(Math.random() * this.boardSize); 102 | col = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1)); 103 | } else { // vertical 104 | row = Math.floor(Math.random() * (this.boardSize - this.shipLength + 1)); 105 | col = Math.floor(Math.random() * this.boardSize); 106 | } 107 | 108 | var newShipLocations = []; 109 | for (var i = 0; i < this.shipLength; i++) { 110 | if (direction === 1) { 111 | newShipLocations.push(row + "" + (col + i)); 112 | } else { 113 | newShipLocations.push((row + i) + "" + col); 114 | } 115 | } 116 | return newShipLocations; 117 | }, 118 | 119 | collision: function(locations) { 120 | for (var i = 0; i < this.numShips; i++) { 121 | var ship = this.ships[i]; 122 | for (var j = 0; j < locations.length; j++) { 123 | if (ship.locations.indexOf(locations[j]) >= 0) { 124 | return true; 125 | } 126 | } 127 | } 128 | return false; 129 | } 130 | 131 | }; 132 | 133 | /* 134 | model.fire("53"); // miss 135 | 136 | model.fire("06"); // hit 137 | model.fire("16"); // hit 138 | model.fire("26"); // hit 139 | 140 | model.fire("34"); // hit 141 | model.fire("24"); // hit 142 | model.fire("44"); // hit 143 | 144 | model.fire("12"); // hit 145 | model.fire("11"); // hit 146 | model.fire("10"); // hit 147 | */ 148 | 149 | 150 | // testing parseGuess 151 | function parseGuess(guess) { 152 | var alphabet = ["A", "B", "C", "D", "E", "F", "G"]; 153 | 154 | if (guess === null || guess.length !== 2) { 155 | alert("Oops, please enter a letter and a number on the board."); 156 | } else { 157 | var row = alphabet.indexOf(guess.charAt(0)); 158 | var column = guess.charAt(1); 159 | 160 | if (isNaN(row) || isNaN(column)) { 161 | alert("Oops, that isn't on the board."); 162 | } else if (row < 0 || row >= model.boardSize || 163 | column < 0 || column >= model.boardSize) { 164 | alert("Oops, that's off the board!"); 165 | } else { 166 | return row + column; 167 | } 168 | } 169 | return null; 170 | } 171 | 172 | /* 173 | console.log("Testing parseGuess"); 174 | console.log(parseGuess("A0")); 175 | console.log(parseGuess("B6")); 176 | console.log(parseGuess("G3")); 177 | console.log(parseGuess("H0")); // invalid 178 | console.log(parseGuess("A7")); // invalid 179 | */ 180 | 181 | // testing the controller 182 | 183 | var controller = { 184 | guesses: 0, 185 | 186 | processGuess: function(guess) { 187 | var location = parseGuess(guess); 188 | if (location) { 189 | this.guesses++; 190 | var hit = model.fire(location); 191 | if (hit && model.shipsSunk === model.numShips) { 192 | view.displayMessage("You sank all my battleships, in " + this.guesses + " guesses"); 193 | } 194 | } 195 | } 196 | } 197 | 198 | 199 | // You should see three ships on the board, one miss, and the message 200 | // "You sank all my battleships in 10 guesses" 201 | /* 202 | controller.processGuess("A0"); // miss 203 | 204 | controller.processGuess("A6"); // hit 205 | controller.processGuess("B6"); // hit 206 | controller.processGuess("C6"); // hit 207 | 208 | controller.processGuess("C4"); // hit 209 | controller.processGuess("D4"); // hit 210 | controller.processGuess("E4"); // hit 211 | 212 | controller.processGuess("B0"); // hit 213 | controller.processGuess("B1"); // hit 214 | controller.processGuess("B2"); // hit 215 | */ 216 | 217 | --------------------------------------------------------------------------------