diff --git a/my_exercise/chapter10/cola.html b/my_exercise/chapter10/cola.html new file mode 100644 index 0000000..64e19f1 --- /dev/null +++ b/my_exercise/chapter10/cola.html @@ -0,0 +1,87 @@ + + + + + Webville Cola + + + + + + \ No newline at end of file diff --git a/my_exercise/chapter10/plane.html b/my_exercise/chapter10/plane.html new file mode 100644 index 0000000..0b0cf7b --- /dev/null +++ b/my_exercise/chapter10/plane.html @@ -0,0 +1,130 @@ + + + + + First class functions + + + + + + \ No newline at end of file diff --git a/my_exercise/chapter11/closureExercise.html b/my_exercise/chapter11/closureExercise.html new file mode 100644 index 0000000..7651466 --- /dev/null +++ b/my_exercise/chapter11/closureExercise.html @@ -0,0 +1,53 @@ + + + + + Closure exercise + + + + + + \ No newline at end of file diff --git a/my_exercise/chapter11/counterClosure.html b/my_exercise/chapter11/counterClosure.html new file mode 100644 index 0000000..4b87b56 --- /dev/null +++ b/my_exercise/chapter11/counterClosure.html @@ -0,0 +1,26 @@ + + + + + counter closure + + + + + + \ No newline at end of file diff --git a/my_exercise/chapter11/divClosure.html b/my_exercise/chapter11/divClosure.html new file mode 100644 index 0000000..70e5a0f --- /dev/null +++ b/my_exercise/chapter11/divClosure.html @@ -0,0 +1,45 @@ + + + + + Click me! + + + + + +
+ + \ No newline at end of file diff --git a/my_exercise/chapter11/flyUndefined.html b/my_exercise/chapter11/flyUndefined.html new file mode 100644 index 0000000..edf86d4 --- /dev/null +++ b/my_exercise/chapter11/flyUndefined.html @@ -0,0 +1,31 @@ + + + + + fly undefined + + + + + + \ No newline at end of file diff --git a/my_exercise/chapter11/functionNested.html b/my_exercise/chapter11/functionNested.html new file mode 100644 index 0000000..2e6da58 --- /dev/null +++ b/my_exercise/chapter11/functionNested.html @@ -0,0 +1,38 @@ + + + + + function nested + + + + + + \ No newline at end of file diff --git a/my_exercise/chapter11/lexicalScope.html b/my_exercise/chapter11/lexicalScope.html new file mode 100644 index 0000000..3e53961 --- /dev/null +++ b/my_exercise/chapter11/lexicalScope.html @@ -0,0 +1,26 @@ + + + + + lexical scope + + + + + + \ No newline at end of file diff --git a/my_exercise/chapter11/timerClosure.html b/my_exercise/chapter11/timerClosure.html new file mode 100644 index 0000000..244a0ca --- /dev/null +++ b/my_exercise/chapter11/timerClosure.html @@ -0,0 +1,33 @@ + + + + + Timer closure + + + + + + \ No newline at end of file diff --git a/my_exercise/chapter2/battleship.js b/my_exercise/chapter2/battleship.js new file mode 100644 index 0000000..99e3f1f --- /dev/null +++ b/my_exercise/chapter2/battleship.js @@ -0,0 +1,34 @@ +var randomLoc = Math.floor(Math.random() * 5); +var location1 = randomLoc; +var location2 = location1 + 1; +var location3 = location2 + 1; + +var guess; +var hits = 0; +var guesses = 0; +var isSunk = false; + +while (isSunk == false) { + guess = prompt("Ready, aim, fire! (enter a number 0-6):"); + if (guess < 0 || guess > 6) { // 检查用户的猜测 + alert("Please enter a valid cell number!"); + } else { + guesses = guesses + 1; // 用户的猜测是有效的,因此将猜测次数加1 + } + + // 如果用户猜测的是战舰位置之一,就将击中计数器加1 + if (guess == location1 || guess == location2 || guess == location3) { + alert("HIT!"); + hits = hits + 1; + if (hits == 3) { //首先检查是否击中了三次 + isSunk = true; + alert("You sank my battleship!"); + } + } else { + alert("MISS"); + } +} + +var stats = "You took " + guesses + " guesses to sink the battleship, " + + "which means your shooting accuracy was " + (3/guesses); +alert(stats); \ No newline at end of file diff --git a/my_exercise/chapter2/test.html b/my_exercise/chapter2/test.html new file mode 100644 index 0000000..9473267 --- /dev/null +++ b/my_exercise/chapter2/test.html @@ -0,0 +1,60 @@ + + + + + Title + + + + + + \ No newline at end of file diff --git a/my_exercise/chapter8/battleship.html b/my_exercise/chapter8/battleship.html new file mode 100644 index 0000000..906b4e4 --- /dev/null +++ b/my_exercise/chapter8/battleship.html @@ -0,0 +1,141 @@ + + + + + Battleship + + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + +
+
+ + + \ No newline at end of file diff --git a/my_exercise/chapter8/battleship.js b/my_exercise/chapter8/battleship.js new file mode 100644 index 0000000..a76af9c --- /dev/null +++ b/my_exercise/chapter8/battleship.js @@ -0,0 +1,155 @@ +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, + shipsSunk: 0, + shipLength: 3, + ships: [{locations: ["0", "0", "0"], hits: ["", "", ""]}, + {locations: ["0", "0", "0"], hits: ["", "", ""]}, + {locations: ["0", "0", "0"], 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"; + 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."); + }, + isSunk: function (ship) { + for (var i = 0; i < this.shipLength; i++) { + if (ship.hits[i] !== "hit") { + return false; + } + } + return true; + }, + generateShipLocations: function () { + var locations; + for (var i = 0; i < this.numShips; i++) { + do { + locations = this.generateShip(); + } while(this.collision(locations)); + this.ships[i].locations = locations; + } + }, + generateShip: function () { + var direction = Math.floor(Math.random() * 2); + var row, col; + + if (direction === 1) { + row = Math.floor(Math.random() * this.boardSize); + col = Math.floor(Math.random() * (this.boardSize - this.shipLength)); + } else { + row = Math.floor(Math.random() * (this.boardSize - this.shipLength)); + col = Math.floor(Math.random() * this.boardSize); + } + + var newShipLocations = []; + for (var i = 0; i < this.shipLength; i++) { + if (direction === 1) { + newShipLocations.push(row + "" + (col + i)); + } else { + newShipLocations.push((row + 1) + "" + col); + } + } + return newShipLocations; + }, + collision: function (locations) { + for (var i = 0; i < this.numShips; i++) { + var ship = this.ships[i]; + for (var j = 0; j < locations.length; j++) { + if (ship.locations.indexOf(locations[j]) >= 0) { + return true; + } + } + } + return false; + } +}; + +var controller = { + guesses: 0, // 猜测次数 + processGuess: function (guess) { // 将一个格式为“A0”的猜测位置作为参数 + 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"); + } + } + } +}; + +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 { + var firstCharAt = guess.charAt(0); + var row = alphabet.indexOf(firstCharAt); + var column = guess.charAt(1); + + if (isNaN(row) || isNaN(column)) { + alert("Oops, that isn't 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; + } +}; + +function init() { + var fireButton = document.getElementById("fireButton"); + fireButton.onclick = handleFireButton; + var guessInput = document.getElementById("guessInput"); + guessInput.onkeypress = handleKeyPress; + + model.generateShipLocations();// 生成战舰 +}; + +function handleFireButton() { + var guessInput = document.getElementById("guessInput"); + var guess = guessInput.value; + controller.processGuess(guess); + + guessInput.value = ""; +}; + +function handleKeyPress(e) { + var fireButton = document.getElementById("fireButton"); + if (e.keyCode === 13) { + fireButton.click(); + return false; + } +} + + +window.onload = init; \ No newline at end of file diff --git a/my_exercise/chapter8/board.jpg b/my_exercise/chapter8/board.jpg new file mode 100644 index 0000000..f1616c1 Binary files /dev/null and b/my_exercise/chapter8/board.jpg differ diff --git a/my_exercise/chapter8/miss.png b/my_exercise/chapter8/miss.png new file mode 100644 index 0000000..471b3a6 Binary files /dev/null and b/my_exercise/chapter8/miss.png differ diff --git a/my_exercise/chapter8/ship.png b/my_exercise/chapter8/ship.png new file mode 100644 index 0000000..955a7e5 Binary files /dev/null and b/my_exercise/chapter8/ship.png differ