├── .gitignore ├── Ch01_Programming_JS_jsBin ├── chapter1.md └── listing1.01.js ├── Ch02_Variables ├── chapter2.md ├── listing2.01.js ├── listing2.02.js ├── listing2.03.js ├── listing2.04.js ├── listing2.05.js ├── listing2.06.js ├── listing2.07.js ├── listing2.08.js └── listing2.09.js ├── Ch03_Objects ├── chapter3.md ├── listing3.01.js ├── listing3.02.js ├── listing3.03.js ├── listing3.04.js ├── listing3.05.js ├── listing3.06.js ├── listing3.07.js ├── listing3.08.js ├── listing3.09.js ├── listing3.10.js ├── listing3.11.js ├── listing3.12.js ├── listing3.13.js ├── listing3.14.js ├── listing3.15.js └── listing3.16.js ├── Ch04_Functions ├── chapter4.md ├── listing4.01.js ├── listing4.02.js ├── listing4.03.js ├── listing4.04.js ├── listing4.05.js ├── listing4.06.js ├── listing4.07.js ├── listing4.08.js ├── listing4.09.js ├── listing4.10.js ├── listing4.11.js ├── listing4.12.js ├── listing4.13.js └── listing4.14.js ├── Ch05_Arguments ├── chapter5.md ├── listing5.01.js ├── listing5.02.js ├── listing5.03.js ├── listing5.04.js ├── listing5.05.js ├── listing5.06.js ├── listing5.07.js ├── listing5.08.js ├── listing5.09.js ├── listing5.10.js ├── listing5.11.js ├── listing5.12.js ├── listing5.13.js └── listing5.14.js ├── Ch06_Return_values ├── chapter6.md ├── listing6.01.js ├── listing6.02.js ├── listing6.03.js ├── listing6.04.js ├── listing6.05.js ├── listing6.06.js ├── listing6.07.js ├── listing6.08.js ├── listing6.09.js └── listing6.10.js ├── Ch07_Object_arguments ├── chapter7.md ├── listing7.01.js ├── listing7.02.js ├── listing7.03.js ├── listing7.04.js ├── listing7.05.js ├── listing7.06.js ├── listing7.07.js ├── listing7.08.js ├── listing7.09.js ├── listing7.10.js └── listing7.11.js ├── Ch08_Arrays ├── chapter8.md ├── listing8.01.js ├── listing8.02.js ├── listing8.03.js ├── listing8.04.js ├── listing8.05.js ├── listing8.06.js ├── listing8.07.js ├── listing8.08.js ├── listing8.09.js ├── listing8.10.js ├── listing8.11.js ├── listing8.12.js └── listing8.13.js ├── Ch09_Constructors ├── chapter9.md ├── listing9.01.js ├── listing9.02.js ├── listing9.03.js ├── listing9.04.js ├── listing9.05.js ├── listing9.06.js ├── listing9.07.js ├── listing9.08.js ├── listing9.09.js ├── listing9.10.js ├── listing9.11.js └── listing9.12.js ├── Ch10_Bracket_notation ├── chapter10.md ├── listing10.01.js ├── listing10.02.js ├── listing10.03.js ├── listing10.04.js ├── listing10.05.js ├── listing10.06.js ├── listing10.07.js ├── listing10.08.js ├── listing10.09.js ├── listing10.10.js ├── listing10.11.js ├── listing10.12_10.13.js └── listing10.14.js ├── Ch11_Scope ├── chapter11.md ├── listing11.01.js ├── listing11.02.js ├── listing11.03.js ├── listing11.04.js ├── listing11.05.js ├── listing11.06.js ├── listing11.07.js ├── listing11.08.js ├── listing11.09.js ├── listing11.10.js ├── listing11.11.js ├── listing11.12.js ├── listing11.13.js ├── listing11.14.js ├── listing11.15.js ├── listing11.16.js ├── listing11.17.js └── listing11.18.js ├── Ch12_Conditions ├── chapter12.md ├── listing12.01.js ├── listing12.02.js ├── listing12.03.js ├── listing12.04.js ├── listing12.05.js ├── listing12.06.js ├── listing12.07.js └── listing12.08.js ├── Ch13_Modules ├── between.js ├── chapter13.md ├── listing13.01.html ├── listing13.02.js ├── listing13.03.html ├── listing13.04.js ├── listing13.05.html ├── listing13.06.js ├── listing13.07.html ├── listing13.08.js ├── listing13.09.html ├── listing13.10.js ├── listing13.11.js ├── listing13.12.js ├── listing13.13.html ├── listing13.14.js ├── listing13.15.js ├── listing13.16.js ├── listing13.17.html ├── listing13.18.js ├── listing13.19.js └── listing13.20.js ├── Ch14_Models ├── TheSparrow.html ├── TheSparrow.js ├── TheSparrowData.js ├── chapter14.md ├── listing14.01.js ├── listing14.02.html ├── listing14.03.js ├── listing14.04.js ├── listing14.05.js ├── listing14.06.js ├── listing14.07.js └── listing14.08.html ├── Ch15_Views ├── Listing 15.05.js ├── Listing15.01.js ├── Listing15.02.html ├── Listing15.03.js ├── Listing15.04.js ├── Listing15.06.js ├── Listing15.07.js ├── Listing15.08.js └── chapter15.md ├── Ch16_Controllers ├── Listing16.12.js ├── chapter16.md ├── listing16.01.js ├── listing16.02.html ├── listing16.03.js ├── listing16.11.html └── listings16.04to16.10.js ├── Ch17_HTML ├── chapter17.md ├── listing17.01.html ├── listing17.02.html ├── listing17.03.html ├── listing17.04.js ├── listing17.05.html ├── listing17.06.js ├── listing17.07.js ├── listing17.08.js ├── listing17.09.js ├── listing17.10.css └── listing17.11.js ├── Ch18_Controls ├── chapter18.md ├── listing18.01.html ├── listing18.02.js ├── listing18.03.html ├── listing18.04.js ├── listing18.05.js ├── listing18.06.html ├── listing18.07.html ├── listing18.08.js ├── listing18.09.js ├── theCrypt.html └── theCrypt.js ├── Ch19_Templates ├── chapter19.md ├── listing19.01.html ├── listing19.02.js ├── listing19.03.js ├── listing19.04.js ├── listing19.05.js ├── listing19.06.js ├── listing19.07.js ├── listing19.08.js ├── listing19.09.js ├── listing19.10.js ├── listing19.11.html ├── listing19.12.js ├── listing19.13.html ├── listing19.14.js ├── listing19.15.js └── listing19.16.js ├── Ch20_XHR ├── chapter20.md ├── listing20.01.js ├── listing20.02.js ├── listing20.03.js ├── listing20.04.js ├── listing20.05.html ├── listing20.06.js ├── listing20.07.json ├── listing20.08.js ├── listing20.09.js ├── listing20.10.html └── listing20.11.js ├── Ch21_Conclusion ├── TheCrypt │ ├── css │ │ └── theCrypt.css │ ├── img │ │ ├── rock2.jpg │ │ └── sand2.jpg │ ├── js │ │ ├── commands.js │ │ ├── gameController.js │ │ ├── mapManager.js │ │ ├── messageView.js │ │ ├── place.js │ │ ├── placeView.js │ │ ├── player.js │ │ ├── playerView.js │ │ └── template.js │ ├── maps │ │ └── TheDarkHouse.js │ └── theCrypt.html └── chapter21.md ├── Ch22_Node ├── between.js ├── chapter22.md ├── fruit.js ├── hello.js ├── jahvers-crypt │ ├── lib │ │ ├── game.js │ │ ├── mapBuilder.js │ │ ├── place.js │ │ ├── player.js │ │ └── playerGame.js │ ├── maps │ │ └── theDarkHouse.json │ └── replTest │ │ └── playerGameTest.js ├── multifruit.js ├── multifruitWhile.js ├── quiz.js └── quiz2.js ├── Ch23_Express ├── TheCrypt │ ├── gameServer.js │ ├── lib │ │ ├── game.js │ │ ├── mapBuilder.js │ │ ├── place.js │ │ ├── player.js │ │ └── playerGame.js │ ├── maps │ │ └── theDarkHouse.json │ ├── package.json │ └── public │ │ ├── css │ │ └── theCrypt.css │ │ ├── img │ │ ├── rock.jpg │ │ └── sand.jpg │ │ ├── jahvers-crypt.html │ │ └── js │ │ ├── commands.js │ │ ├── dataLoader-xhr.js │ │ ├── gameController.js │ │ ├── messageView.js │ │ ├── placeView.js │ │ ├── playerView.js │ │ └── templates.js ├── chapter23.md └── webserver │ ├── fitnessServer.js │ ├── hello.js │ ├── package.json │ ├── public │ ├── adventure.html │ ├── client-server-GET.png │ ├── client-server.html │ ├── fitnessData.html │ ├── index.html │ ├── js │ │ └── xhrPost.js │ └── moon.html │ ├── static.js │ └── threeRoutes.js ├── Ch24_Polling ├── HighFives_XHR │ ├── auctionApp.js │ ├── package.json │ └── public │ │ ├── auction.css │ │ ├── auction.html │ │ └── auction.js └── TheCrypt │ ├── gameServer.js │ ├── lib │ ├── game.js │ ├── gameManager.js │ ├── mapBuilder.js │ ├── place.js │ ├── player.js │ └── playerGame.js │ ├── maps │ └── theDarkHouse.json │ ├── package.json │ └── public │ ├── css │ └── theCrypt.css │ ├── img │ ├── rock.jpg │ └── sand.jpg │ ├── jahvers-crypt-xhr.html │ └── js │ ├── commands.js │ ├── dataLoader-xhr.js │ ├── gameController.js │ ├── mainView.js │ ├── messageView.js │ ├── placeView.js │ ├── playerView.js │ ├── startGame.js │ └── templates.js ├── Ch25_Sockets ├── HighFives_Sockets │ ├── auctionApp.js │ ├── package.json │ └── public │ │ ├── auction.css │ │ ├── auction.html │ │ └── auction.js ├── HighFives_SocketsRooms │ ├── auctionApp.js │ ├── package.json │ └── public │ │ ├── auction.css │ │ ├── auction.html │ │ └── auction.js └── TheCrypt │ ├── gameServer.js │ ├── lib │ ├── game.js │ ├── gameManager.js │ ├── mapBuilder.js │ ├── place.js │ ├── player.js │ ├── playerGame.js │ └── socketManager.js │ ├── maps │ └── theDarkHouse.json │ ├── package.json │ └── public │ ├── css │ └── theCrypt.css │ ├── img │ ├── rock.jpg │ └── sand.jpg │ ├── jahvers-crypt-socketio.html │ └── js │ ├── commands.js │ ├── dataLoader-socketio.js │ ├── gameController.js │ ├── mainView.js │ ├── messageView.js │ ├── placeView.js │ ├── playerView.js │ ├── startGame.js │ └── templates.js ├── README.md ├── js_next ├── README.md ├── listing2.10.js ├── listing2.11.js ├── listing2.12.js ├── listing3.17.js ├── listing3.18.js ├── listing3.19.js ├── listing3.20.js ├── listing4.15.js ├── listing4.16.js ├── listing4.17.js ├── listing5.15.js ├── listing5.16.js ├── listing5.17.js ├── listing5.18.js ├── listing5.19.js ├── listing6.11.js ├── listing6.12.js ├── listing6.13.js ├── listing6.14.js ├── listing7.12.js ├── listing7.13.js ├── listing7.14.js └── listing7.15.js └── projects └── theCrypt_Node ├── api └── index.js ├── index.js ├── lib ├── game.js ├── gameCache.js ├── gameController.js ├── mapBuilder.js ├── place.js └── player.js ├── maps └── theDarkHouse.json ├── package.json └── public ├── css └── theCrypt.css ├── img ├── rock.jpg └── sand.jpg ├── js ├── commands.js ├── dataLoader.js ├── gameController.js ├── messageView.js ├── placeView.js ├── playerView.js └── templates.js ├── maps └── TheDarkHouse.js └── theCrypt.html /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .idea/ -------------------------------------------------------------------------------- /Ch01_Programming_JS_jsBin/chapter1.md: -------------------------------------------------------------------------------- 1 | # Programming, JavaScript and jsBin 2 | 3 | ### Using console.log 4 | [JS Bin: Listing 1.01](http://jsbin.com/mujepu/edit?js,console) - mujepu 5 | ```javascript 6 | /* Get Programming with JavaScript 7 | * Listing 1.01 8 | * Using console.log to display information 9 | */ 10 | 11 | console.log("Hello World!"); 12 | 13 | 14 | 15 | /* Further Adventures 16 | * 17 | * 1) Change the text between the parentheses. 18 | * 19 | * 2) Click Run on the console to run the 20 | * program again. 21 | * 22 | * 3) Add your own console.log statement 23 | * to the program, so there are two. 24 | * Don't forget the semicolon. 25 | * 26 | * 4) Click Run. 27 | * 28 | */ 29 | ``` 30 | -------------------------------------------------------------------------------- /Ch01_Programming_JS_jsBin/listing1.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 1.01 3 | * Using console.log to display information 4 | */ 5 | 6 | console.log("Hello World!"); 7 | 8 | 9 | 10 | /* Further Adventures 11 | * 12 | * 1) Change the text between the parentheses. 13 | * 14 | * 2) Click Run on the console to run the 15 | * program again. 16 | * 17 | * 3) Add your own console.log statement 18 | * to the program, so there are two. 19 | * Don't forget the semicolon. 20 | * 21 | * 4) Click Run. 22 | * 23 | */ 24 | -------------------------------------------------------------------------------- /Ch02_Variables/listing2.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 2.01 3 | * Declaring a variable 4 | */ 5 | 6 | var score; 7 | 8 | 9 | 10 | /* Further Adventures 11 | * 12 | * 1) Delete the semicolon at after the word score. 13 | * JS Bin should complain and show an error message. 14 | * Take a look at the message. 15 | * 16 | * 2) Add the semicolon back after the word score. 17 | * The semicolon shows the end of the statement. 18 | * 19 | * 3) Declare two more variables. 20 | * 21 | */ 22 | -------------------------------------------------------------------------------- /Ch02_Variables/listing2.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 2.02 3 | * Assigning a value to a variable 4 | */ 5 | 6 | var score; 7 | score = 100; 8 | 9 | 10 | 11 | /* Further Adventures 12 | * 13 | * 1) Click Run on the JS Bin Console panel. 14 | * Nothing will be displayed on the console 15 | * but the program will have run, 16 | * creating the score variable. 17 | * 18 | * 2) Click in the console, just to the right of the blue prompt > 19 | * The console should be highlighted and 20 | * a cursor should be blinking next to the prompt. 21 | * 22 | * 3) Type the word score to match your variable name 23 | * and press enter. 24 | * 100, the value assigned to score, should be displayed. 25 | * 26 | * 4) Change the value of score in the code above. 27 | * Repeat steps 1, 2 and 3. 28 | * 29 | */ 30 | -------------------------------------------------------------------------------- /Ch02_Variables/listing2.03.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 2.03 3 | * Using a variable 4 | */ 5 | 6 | var score; 7 | score = 100; 8 | console.log(score); 9 | 10 | 11 | 12 | /* Further Adventures 13 | * 14 | * 1) Change the value assigned to score. 15 | * Run the program again. 16 | * 17 | * 2) Declare a second variable, maybe score2. 18 | * 19 | * 3) Assign your variable a value. 20 | * 21 | * 4) Use console.log to display your variable on the console. 22 | * 23 | */ 24 | -------------------------------------------------------------------------------- /Ch02_Variables/listing2.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 2.04 3 | * Variables vary 4 | */ 5 | 6 | var score; 7 | score = 100; 8 | console.log(score); 9 | 10 | score = 150; 11 | console.log(score); 12 | 13 | 14 | 15 | /* Further Adventures 16 | * 17 | * 1) Change the value assigned to score. 18 | * Run the program again. 19 | * 20 | * 2) Declare a second variable, maybe score2. 21 | * 22 | * 3) Assign your variable a value. 23 | * 24 | * 4) Use console.log to display your 25 | * variable on the console. 26 | * 27 | * 5) Add a new line of code to alter the 28 | * value of your variable after it 29 | * has been displayed on the console. 30 | * 31 | * 6) Add a new line of code to display 32 | * the new value of your variable. 33 | * 34 | */ 35 | -------------------------------------------------------------------------------- /Ch02_Variables/listing2.05.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 2.05 3 | * Assigning text to variables 4 | */ 5 | 6 | var message; 7 | 8 | message = "Hello World!"; 9 | console.log(message); 10 | 11 | message = 'Congratulations! Your tweet has won a prize...'; 12 | console.log(message); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Assign message a third value. Display it on the console. 19 | * 20 | * Pieces of text can be joined together using the + symbol. 21 | * e.g. "Hello " + "World!" 22 | * 23 | * 2) Split the first message into two pieces, 24 | * joined by + and run the program. 25 | * 26 | * 3) Split the second message into three pieces, 27 | * joined by + and run the program. 28 | * Be careful to use matching speech marks. 29 | * 30 | */ 31 | -------------------------------------------------------------------------------- /Ch02_Variables/listing2.06.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 2.06 3 | * Declaring and assigning in two steps 4 | */ 5 | 6 | var playerName; 7 | var locationName; 8 | 9 | playerName = "Kandra"; 10 | locationName = "The Dungeon of Doom"; 11 | 12 | console.log(playerName + " is in " + locationName); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Declare a score variable. 19 | * 20 | * 2) Assign it a value. 21 | * 22 | * 3) Display an extra message on the console, 23 | * by combining variables and strings, to say 24 | * something like "Kandra has a score of 100" 25 | * 26 | */ 27 | -------------------------------------------------------------------------------- /Ch02_Variables/listing2.07.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 2.07 3 | * Declaring and assigning in one step 4 | */ 5 | 6 | var playerName = "Kandra"; 7 | var locationName = "The Dungeon of Doom"; 8 | 9 | console.log(playerName + " is in " + locationName); 10 | 11 | 12 | 13 | /* Further Adventures 14 | * 15 | * 1) Declare a health variable and assign it 16 | * a value in one step. 17 | * 18 | * 2) Declare a message variable. 19 | * 20 | * 3) Assign message a value created by joining 21 | * variables and text to say something like 22 | * "Kandra has health 50 and is in The Dungeon of Doom" 23 | * 24 | * 4) Replace the current console.log statement 25 | * with a new one to display the message variable. 26 | * 27 | */ 28 | -------------------------------------------------------------------------------- /Ch02_Variables/listing2.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 2.08 3 | * Using a variable’s current value to set its new value 4 | */ 5 | 6 | var score; 7 | score = 100; 8 | console.log("Your score was " + score); 9 | 10 | console.log("Great splat!!!"); 11 | score = score + 50; 12 | console.log("New score: " + score); 13 | 14 | console.log("Way to go!"); 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * The player splats a kumquat. 21 | * Add code to: 22 | * 23 | * 1) Tell the player of a successful splat 24 | * 25 | * 2) Add 100 to the player's score 26 | * 27 | * 3) Show the new score 28 | * 29 | * 4) Display a message of congratulations 30 | * 31 | */ 32 | -------------------------------------------------------------------------------- /Ch02_Variables/listing2.09.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 2.09 3 | * Valid and invalid variable names 4 | */ 5 | 6 | var thisIsFine; 7 | var $noProblemHere; 8 | var _underscore56; 9 | var StartWithCapital; 10 | var z5; 11 | 12 | var 999; 13 | var 39Steps; 14 | var &nope; 15 | var single words only; 16 | var yield; 17 | 18 | 19 | 20 | /* Further Adventures 21 | * 22 | * 1) Take a look at the errors in the 23 | * error section of JS Bin below. 24 | * 25 | * 2) Click Run in the console 26 | * and see what errors appear there. 27 | * 28 | * 3) Delete the second block of variables 29 | * one by one from the code. 30 | * See how the errors change. 31 | * 32 | */ 33 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.01 3 | * Using variables to represent a book 4 | */ 5 | 6 | var bookTitle; 7 | var bookAuthor; 8 | 9 | bookTitle = "The Hobbit"; 10 | bookAuthor = "J. R. R. Tolkien"; 11 | 12 | console.log(bookTitle + " by " + bookAuthor); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Declare variables for a second book 19 | * and assign them values. 20 | * 21 | * 2) Add code to log its details to the console. 22 | * 23 | * 3) Repeat steps 1 and 2 for a third book. 24 | * 25 | * 4) Consider the code needed for 10 books. 26 | * For 100 books. 27 | * 28 | */ 29 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.02 3 | * Using prefixes to tell book variables apart 4 | */ 5 | 6 | var book1Title = "The Hobbit"; 7 | var book1Author = "J.R.R.Tolkien"; 8 | 9 | var book2Title = "Northern Lights"; 10 | var book2Author = "Philip Pullman"; 11 | 12 | var book3Title = "The Adventures of Tom Sawyer"; 13 | var book3Author = "Mark Twain"; 14 | 15 | console.log("There are three books so far..."); 16 | console.log(book1Title + " by " + book1Author); 17 | console.log(book2Title + " by " + book2Author); 18 | console.log(book3Title + " by " + book3Author); 19 | 20 | 21 | 22 | /* Further Adventures 23 | * 24 | * 1) Add a fourth book. 25 | * 26 | * 2) Update the messages to include the new book. 27 | * 28 | */ 29 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.03.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.03 3 | * A book as an object 4 | */ 5 | 6 | var book; 7 | 8 | book = { 9 | title : "The Hobbit", 10 | author : "J. R. R. Tolkien", 11 | published : 1937 12 | }; 13 | 14 | console.log(book); 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Change the title of book to 21 | * "The Hobbit, or There and Back Again". 22 | * Run the program again. 23 | * 24 | * 2) Try creating a book2 object. 25 | * 26 | * 3) Log book2 to the console. 27 | * 28 | */ 29 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.04 3 | * Creating an empty object 4 | */ 5 | 6 | var book; 7 | 8 | book = {}; 9 | 10 | 11 | 12 | /* Further Adventures 13 | * 14 | * 1) Log book to the console. 15 | * 16 | * 2) Place your cursor between the curly braces 17 | * in the code above and press enter. 18 | * JS Bin should add an empty line between 19 | * the braces. You can add more empty lines. 20 | * 21 | * 3) Run the program again, logging book 22 | * to the console. 23 | * The extra lines should make no difference 24 | * to the outcome. 25 | * 26 | */ 27 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.05.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.05 3 | * An object with a single property 4 | */ 5 | 6 | var book; 7 | 8 | book = { 9 | title : "The Hobbit" 10 | }; 11 | 12 | 13 | 14 | /* Further Adventures 15 | * 16 | * 1) Log book to the console. 17 | * 18 | * 2) Create a second book. 19 | * 20 | * 3) Log book2 to the console. 21 | * 22 | * 4) Run the program. 23 | * In the console tab, click after the prompt, >. 24 | * Type book and press Enter. 25 | * The book object should be displayed. 26 | * 27 | */ 28 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.06.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.06 3 | * Using a variable as a property value 4 | */ 5 | 6 | var book; 7 | var bookName; 8 | 9 | bookName = "The Adventures of Tom Sawyer"; 10 | 11 | book = { 12 | title : bookName 13 | }; 14 | 15 | 16 | 17 | /* Further Adventures 18 | * 19 | * 1) Run the program. 20 | * 21 | * 2) At the console prompt, type 'book' 22 | * and press enter. 23 | * 24 | * The book object should be displayed on the console. 25 | * Its title property should have the same 26 | * value as the bookName variable. 27 | * 28 | * 3) Change the value of the bookName variable 29 | * and try steps 1 and 2 again. 30 | * 31 | */ -------------------------------------------------------------------------------- /Ch03_Objects/listing3.07.js: -------------------------------------------------------------------------------- 1 | /* Get Programmimg with JavaScript 2 | * Listing 3.07 3 | * Objects with multiple properties 4 | */ 5 | 6 | var book1; 7 | var book2; 8 | 9 | book1 = { 10 | title : "The Hobbit", 11 | author : "J. R. R. Tolkien" 12 | }; 13 | 14 | book2 = { 15 | title : "Northern Lights", 16 | author : "Philip Pullman" 17 | }; 18 | 19 | 20 | 21 | /* Further Adventures 22 | * 23 | * 1) Add a third property to each book. 24 | * Don't forget the commas. 25 | * 26 | * 2) Add spaces so that the colons line up. 27 | * 28 | * 3) Log both objects to the console. 29 | * 30 | */ -------------------------------------------------------------------------------- /Ch03_Objects/listing3.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.08 3 | * Using dot notation to access property values 4 | */ 5 | 6 | var book; 7 | 8 | book = { 9 | title : "The Hobbit", 10 | author : "J. R. R. Tolkien", 11 | published : 1937 12 | }; 13 | 14 | console.log(book.title); 15 | console.log(book.author); 16 | 17 | 18 | 19 | /* Further Adventures 20 | * 21 | * 1) Log the published property to the console. 22 | * 23 | * 2) Add a second book. 24 | * 25 | * 3) Log its details to the console. 26 | * 27 | * 4) In the console tab, at the prompt, 28 | * type in book.title and press Enter. 29 | * Once the program is run, object properties 30 | * are available interactively via the console. 31 | * 32 | */ 33 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.09.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.09 3 | * Concatenating string properties 4 | */ 5 | 6 | var book1; 7 | var book2; 8 | 9 | book1 = { 10 | title: "The Hobbit", 11 | author: "J. R. R. Tolkien" 12 | }; 13 | 14 | book2 = { 15 | title: "Northern Lights", 16 | author: "Philip Pullman" 17 | }; 18 | 19 | console.log(book1.title + " by " + book1.author); 20 | console.log(book2.title + " by " + book2.author); 21 | 22 | 23 | 24 | /* Further Adventures 25 | * 26 | * 1) Add a third book. 27 | * 28 | * 2) Log its details to the console. 29 | * 30 | * 3) Add a third property. 31 | * 32 | * 4) Update the messages logged to include the 33 | * new property. 34 | * 35 | */ 36 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.10.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.10 3 | * Using dot notation to update a property 4 | */ 5 | 6 | var player1; 7 | 8 | player1 = { 9 | name: "Max", 10 | attempted: 0, 11 | correct: 0 12 | }; 13 | 14 | player1.attempted = 1; 15 | player1.correct = 1; 16 | player1.score = 50; 17 | 18 | 19 | 20 | /* Further Adventures 21 | * 22 | * 1) Run the program and then 23 | * click after the console prompt. 24 | * 25 | * 2) Change the value of the score property 26 | * by typing player1.score = 100; 27 | * at the prompt and pressing Enter. 28 | * 29 | * 3) Update other properties using the console. 30 | * 31 | */ 32 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.11.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.11 3 | * Using a property in a calculation 4 | */ 5 | 6 | var player1; 7 | 8 | player1 = { 9 | name: "Max", 10 | score: 0 11 | }; 12 | 13 | console.log(player1.name + " has scored " + player1.score); 14 | 15 | player1.score = player1.score + 50; 16 | 17 | console.log(player1.name + " has scored " + player1.score); 18 | 19 | 20 | 21 | /* Further Adventures 22 | * 23 | * 1) Write code to increase player1's score by 10%. 24 | * 25 | * 2) Add a second player. 26 | * 27 | * 3) Use the players' properties to find the sum 28 | * of their scores and log it to the console, 29 | * along with an appropriate message that includes 30 | * their names. 31 | * 32 | */ 33 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.12.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.12 3 | * A blog post 4 | */ 5 | 6 | var post = { 7 | id : 1, 8 | title : "My Crazy Space Adventure", 9 | author : "Philae", 10 | created : "2015-06-21", 11 | body : "You will not believe where I just woke up!! Only on a comet..." 12 | }; 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Display some of the properties. 19 | * 20 | * 2) Add a second post. 21 | * 22 | */ 23 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.13.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.13 3 | * A calendar event 4 | */ 5 | 6 | var event = { 7 | title : "Appraisal Meeting", 8 | startDate : "2016-10-04 16:00:00", 9 | endDate : "2016-10-04 17:00:00", 10 | location : "Martha's office", 11 | importance: 1, 12 | notes : 'Don\'t forget the portfolio!' 13 | }; 14 | 15 | 16 | 17 | /* Further Adventures 18 | * 19 | * 1) Remove the backslash from the notes property. 20 | * 21 | * 2) Take a look at the errors reported by JS Bin. 22 | * 23 | * 3) Put the backslash back. 24 | * 25 | * 4) See how the location property doesn't need 26 | * a backslash. What happens if you add one and 27 | * log the location property? 28 | * 29 | * 5) What if you want to show a backslash 30 | * as part of a string? 31 | * 32 | */ 33 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.15.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.15 3 | * A question and answer for a quiz app 4 | */ 5 | 6 | var questionAndAnswer = { 7 | question: "What is the capital of France?", 8 | answer1: "Bordeaux", 9 | answer2: "F", 10 | answer3: "Paris", 11 | answer4: "Brussels", 12 | correctAnswer: "Paris", 13 | marksForQuestion: 2 14 | }; 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Use console.log to present the question 21 | * and answer options in a nicely formatted way. 22 | * 23 | */ 24 | -------------------------------------------------------------------------------- /Ch03_Objects/listing3.16.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 3.16 3 | * A player object 4 | */ 5 | 6 | var player; 7 | 8 | player = { 9 | name: "Kandra", 10 | health: 50, 11 | place: "The Dungeon of Doom", 12 | items: "a rusty key, The Sword of Destiny, a piece of cheese" 13 | }; 14 | 15 | console.log(player.name); 16 | console.log(player.name + " is in " + player.place); 17 | console.log(player.name + " has health " + player.health); 18 | console.log("Items: " + player.items); 19 | 20 | 21 | 22 | /* Further Adventures 23 | * 24 | * 1) Create a second player. 25 | * 26 | * 2) Log their details to the console. 27 | * 28 | * Notice how storing the items as a string 29 | * isn't a great approach. If the player 30 | * drops an item you'll have to manipulate 31 | * the string to remove the item. 32 | * 33 | * JavaScript has a way of storing lists 34 | * of items called an array. See Chapter 8. 35 | * 36 | */ 37 | -------------------------------------------------------------------------------- /Ch04_Functions/listing4.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 4.01 3 | * Displaying an object's properties on the console 4 | */ 5 | 6 | var movie1; 7 | 8 | movie1 = { 9 | title: "Inside Out", 10 | actors: "Amy Poehler, Bill Hader", 11 | directors: "Pete Doctor, Ronaldo Del Carmen" 12 | }; 13 | 14 | console.log("Movie information for " + movie1.title); 15 | console.log("------------------------------"); 16 | console.log("Actors: " + movie1.actors); 17 | console.log("Directors: " + movie1.directors); 18 | console.log("------------------------------"); 19 | 20 | 21 | 22 | /* Further Adventures 23 | * 24 | * 1) Add a second movie and display the same info for it. 25 | * 26 | * 2) Create an object to represent a blog post. 27 | * 28 | * 3) Write code to display info about the blog post. 29 | * 30 | */ 31 | -------------------------------------------------------------------------------- /Ch04_Functions/listing4.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 4.04 3 | * A simple function definition and assignment 4 | */ 5 | 6 | var sayHello; 7 | 8 | sayHello = function () { 9 | console.log("Hello World!"); 10 | }; 11 | 12 | 13 | 14 | /* Further Adventures 15 | * 16 | * 1) Run the program. 17 | * 18 | * Nothing will appear on the console. 19 | * You have defined a function. 20 | * You have assigned the function to a variable. 21 | * You have not yet made the function run. 22 | * 23 | * You can make the function run by 24 | * typing the name of the variable it has been 25 | * assigned to, followed by parentheses. 26 | * 27 | * sayHello(); 28 | * 29 | * 3) At the end of the program, add code to 30 | * make the function run. 31 | * 32 | * 4) Run the program. 33 | * 34 | */ -------------------------------------------------------------------------------- /Ch04_Functions/listing4.05.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 4.05 3 | * Two more function definitions and assignments 4 | */ 5 | 6 | var findTotal; 7 | var displayMenu; 8 | 9 | findTotal = function () { 10 | result = number1 + number2; 11 | }; 12 | 13 | displayMenu = function () { 14 | console.log("Please choose an option:"); 15 | console.log("(1) Print log"); 16 | console.log("(2) Upload file"); 17 | console.log("(9) Quit"); 18 | }; 19 | 20 | 21 | 22 | /* Further Adventures 23 | * 24 | * How to run a function, also called calling 25 | * or invoking the function, is explained in 26 | * Section 4.2.3. 27 | * If these exercises are too hard, you 28 | * can always come back to them later. 29 | * 30 | * 1) Declare number1, number2 and result variables. 31 | * 32 | * 2) Assign values to the variables. 33 | * 34 | * 3) Run the findTotal function by writing its name 35 | * followed by parentheses: findTotal(); 36 | * 37 | * 4) Display the result on the console. 38 | * 39 | * 5) Run the displayMenu function. 40 | * 41 | */ 42 | -------------------------------------------------------------------------------- /Ch04_Functions/listing4.06.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 4.06 3 | * Calling the sayHello function three times 4 | */ 5 | 6 | var sayHello; 7 | 8 | sayHello = function () { 9 | console.log("Hello World!"); 10 | }; 11 | 12 | sayHello(); 13 | sayHello(); 14 | sayHello(); 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Change the message from the sayHello function. 21 | * 22 | * To add a line-break to a string, insert '\n' 23 | * e.g. "Line One\nLine Two" 24 | * 25 | * 2) Break the Hello World! message across two lines. 26 | * 27 | * 3) Create a function that prints the letters 28 | * of "Hello World!" one by one down the page 29 | * 30 | */ 31 | -------------------------------------------------------------------------------- /Ch04_Functions/listing4.07.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 4.07 3 | * Using the findTotal function to display a calculation 4 | */ 5 | 6 | var number1 = 1000; 7 | var number2 = 66; 8 | var result; 9 | var findTotal; 10 | 11 | findTotal = function () { 12 | result = number1 + number2; 13 | }; 14 | 15 | findTotal(); 16 | 17 | console.log(number1 + " + " + number2 + " = " + result); 18 | 19 | 20 | 21 | /* Further Adventures 22 | * 23 | * 1) Change number1 and number2 and run the program 24 | * 25 | * 2) Add a third variable, number3, and assign it a value 26 | * 27 | * 3) Update the findTotal function to add the three numbers 28 | * 29 | * 4) Update the console.log line to show the new calculation 30 | * 31 | */ 32 | -------------------------------------------------------------------------------- /Ch04_Functions/listing4.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 4.08 3 | * Displaying a menu 4 | */ 5 | 6 | var displayMenu; 7 | 8 | displayMenu = function () { 9 | console.log("Please choose an option:"); 10 | console.log("(1) Print log"); 11 | console.log("(2) Upload file"); 12 | console.log("(9) Quit"); 13 | }; 14 | 15 | displayMenu(); 16 | 17 | 18 | 19 | /* Further Adventures 20 | * 21 | * 1) Add two more options to the displayMenu 22 | * function and run the program. 23 | * 24 | * 2) Create your own displayMyMenu function. 25 | * 26 | * 3) Add a line to call your function as well. 27 | * Run the program 28 | * 29 | */ -------------------------------------------------------------------------------- /Ch04_Functions/listing4.09.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 4.09 3 | * Using a function to display object properties 4 | */ 5 | 6 | var showMovieInfo; 7 | 8 | showMovieInfo = function () { 9 | console.log("Movie information for " + movie.title); 10 | console.log("------------------------------"); 11 | console.log("Actors: " + movie.actors); 12 | console.log("Directors: " + movie.directors); 13 | console.log("------------------------------"); 14 | }; 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Add a call to showMovieInfo and run the program. 21 | * What happens? 22 | * 23 | * 2) Declare a movie variable 24 | * but don't assign it a value. 25 | * Run the program. What happens now? 26 | * 27 | * 3) Create an empty object and 28 | * assign it to the movie variable. 29 | * Run the program again. 30 | * Does the output change? 31 | * 32 | * 4) Add title, actors and directors properties 33 | * to the movie object. Run one more time. 34 | * Is the output as expected? 35 | * 36 | */ 37 | -------------------------------------------------------------------------------- /Ch04_Functions/listing4.14.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 4.14 3 | * A function to display player information 4 | */ 5 | 6 | var player1; 7 | var player2; 8 | var player; 9 | var showPlayerInfo; 10 | 11 | player1 = { 12 | name: "Kandra", 13 | place: "The Dungeon of Doom", 14 | health: 50 15 | }; 16 | 17 | player2 = { 18 | name: "Dax", 19 | place: "The Old Library", 20 | health: 40 21 | }; 22 | 23 | showPlayerInfo = function () { 24 | console.log(player.name); 25 | console.log("------------------------------"); 26 | console.log(player.name + " is in " + player.place); 27 | console.log(player.name + " has health " + player.health); 28 | console.log("------------------------------"); 29 | console.log(""); 30 | }; 31 | 32 | player = player1; 33 | showPlayerInfo(); 34 | 35 | player = player2; 36 | showPlayerInfo(); 37 | 38 | 39 | 40 | /* Further Adventures 41 | * 42 | * 1) Write a function that just shows 43 | * where the player is. 44 | * 45 | * 2) Write a function that just shows 46 | * the player's health. 47 | * 48 | * 3) Change the showPlayerInfo function 49 | * to use your two functions. 50 | * 51 | */ 52 | -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.01 3 | * Relying on a variable outside of the function 4 | */ 5 | 6 | var message; 7 | var showMessage; 8 | 9 | message = "It's full of stars!"; 10 | 11 | showMessage = function () { 12 | console.log(message); 13 | }; 14 | 15 | showMessage(); 16 | 17 | 18 | 19 | /* Further Adventures 20 | * 21 | * 1) Change the message and run the program. 22 | * 23 | * 2) Change the variable name in the 24 | * parentheses for console.log. 25 | * e.g. console.log(msg); 26 | * 27 | * 3) Run the program 28 | * 29 | * The showMessage function relies on 30 | * the variable name it uses with 31 | * console.log matching one that exists 32 | * outside of the function body. 33 | * 34 | */ 35 | -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.02 3 | * Breaking a function by changing a variable name 4 | */ 5 | 6 | var msg; 7 | var showMessage; 8 | 9 | msg = "It's full of stars!"; 10 | 11 | showMessage = function () { 12 | console.log(message); 13 | }; 14 | 15 | showMessage(); 16 | 17 | 18 | 19 | /* Further Adventures 20 | * 21 | * 1) Update the console.log so the program works. 22 | * 23 | * 2) Declare another message variable 24 | * and assign it a value. 25 | * 26 | * 3) Display the new message as well as the old one. 27 | * 28 | * 4) Display a single message created by joining 29 | * the two strings. 30 | * 31 | */ 32 | -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.03.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.03 3 | * Passing information to a function 4 | */ 5 | 6 | var showMessage; 7 | 8 | showMessage = function (message) { 9 | console.log("The message is: " + message); 10 | }; 11 | 12 | showMessage("It's full of stars!"); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Change the message in the parentheses 19 | * when showMessage is called on line 12. 20 | * 21 | * The value in the parentheses when showMessage is called 22 | * is an argument. "It's full of stars!" was the 23 | * original argument on line 12. 24 | * 25 | * 2) Add two more calls to the showMessage function 26 | * with different arguments each time. 27 | * 28 | * 3) Change the showMessage function itself. 29 | * Make it add some extra text after the 30 | * message passed in. 31 | * 32 | */ -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.04 3 | * Calling the same function with different arguments 4 | */ 5 | 6 | var showMessage; 7 | 8 | showMessage = function (message) { 9 | console.log("The message is: " + message); 10 | }; 11 | 12 | showMessage("It's full of stars!"); 13 | showMessage("Hello to Jason Isaacs"); 14 | showMessage("Hello to Jason Isaacs and Stephen Fry"); 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Change the showMessage function to 21 | * display its prefixed text on a 22 | * separate line to the message. 23 | * 24 | * 2) Declare a myMessage variable and 25 | * assign it a string value. 26 | * 27 | * 3) Call the showMessage function with 28 | * myMessage as the argument. 29 | * 30 | */ -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.05.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.05 3 | * Using the square function 4 | */ 5 | 6 | var square; 7 | 8 | square = function (numberToSquare) { 9 | var result; 10 | result = numberToSquare * numberToSquare; 11 | console.log(numberToSquare + " * " + numberToSquare + " = " + result); 12 | }; 13 | 14 | square(10); 15 | square(-2); 16 | square(1111); 17 | square(0.5); 18 | 19 | 20 | 21 | /* Further Adventures 22 | * 23 | * 1) Define a cube function that cubes 24 | * any number passed in as an argument. 25 | * 26 | * 2) Call your cube function four times 27 | * with different arguments to test it. 28 | * 29 | * Math.sqrt is a built-in function to find 30 | * the square root of a number. 31 | * e.g. Math.sqrt(9) finds the square root of 9. 32 | * 33 | * 3) Define and test a squareRoot function 34 | * to find square roots and display 35 | * them on the console. 36 | * e.g. The square root of 9 is 3. 37 | * 38 | */ 39 | -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.06.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.06 3 | * A function with two arguments 4 | */ 5 | 6 | var showSum; 7 | 8 | showSum = function (number1, number2) { 9 | var total = number1 + number2; 10 | console.log("The sum is " + total); 11 | }; 12 | 13 | showSum(30, 23); 14 | showSum(2.8, -5); 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Use the showSum function to add 56 and 74. 21 | * 22 | * To multiply two numbers, use the * symbol. 23 | * e.g. 3 * 5 is 3 multiplied by 5. 24 | * 25 | * 2) Create a showProduct function to multiply two numbers. 26 | * 27 | * 3) Use your function to multiply three pairs of numbers. 28 | * 29 | * To divide one number by another, use /. 30 | * e.g. 10 / 2 is 10 divided by 2. 31 | * 32 | * 4) What about showDifference and showQuotient 33 | * for subtraction and division? 34 | * 35 | */ -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.07.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.07 3 | * Displaying a player's name 4 | */ 5 | 6 | var showPlayerName; 7 | 8 | showPlayerName = function (playerName) { 9 | console.log(playerName); 10 | }; 11 | 12 | showPlayerName("Kandra"); 13 | showPlayerName("Dax"); 14 | 15 | 16 | 17 | /* Further Adventures 18 | * 19 | * 1) Update the text logged so that it is 20 | * of the form: 21 | * The player's name is Kandra 22 | * 23 | * Strings have a length property that gives 24 | * the number of characters in the string. 25 | * e.g. playerName.length. 26 | * 27 | * 2) Make the function show the number 28 | * of letters in the player's name. 29 | * 30 | */ -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.08 3 | * Displaying a player's name via an object property 4 | */ 5 | 6 | var player1; 7 | var player2; 8 | var showPlayerName; 9 | 10 | showPlayerName = function (playerName) { 11 | console.log(playerName); 12 | }; 13 | 14 | player1 = { 15 | name: "Kandra", 16 | place: "The Dungeon of Doom", 17 | health: 50 18 | }; 19 | 20 | player2 = { 21 | name: "Dax", 22 | place: "The Old Library", 23 | health: 40 24 | }; 25 | 26 | showPlayerName(player1.name); 27 | showPlayerName(player2.name); 28 | 29 | 30 | 31 | /* Further Adventures 32 | * 33 | * 1) In the showPlayerName function, 34 | * change playerName to playerName.toUpperCase(). 35 | * Run the function. 36 | * 37 | * 2) Change the function to show the player's 38 | * name in lower case. 39 | * 40 | */ 41 | -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.09.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.09 3 | * Displaying a player's health 4 | */ 5 | 6 | var showPlayerHealth; 7 | 8 | showPlayerHealth = function (playerName, playerHealth) { 9 | console.log(playerName + " has health " + playerHealth); 10 | }; 11 | 12 | showPlayerHealth("Kandra", 50); 13 | showPlayerHealth("Dax", 40); 14 | 15 | 16 | 17 | /* Further Adventures 18 | * 19 | * 1) Change the showPlayerHealth function 20 | * so it shows information of the form: 21 | * Kandra: health 50. 22 | * 23 | * 2) Call the showPlayerHealth function 24 | * using your own arguments. 25 | * 26 | * 3) Declare a variable called healthInfo 27 | * inside the showPlayerHealth function. 28 | * 29 | * var healthInfo; 30 | * 31 | * 4) Assign healthInfo the string that 32 | * will be displayed. 33 | * 34 | * healthInfo = playerName + ": health " + playerHealth; 35 | * 36 | * 5) Change the call to console.log so that 37 | * it uses the healthInfo variable. 38 | * 39 | */ 40 | -------------------------------------------------------------------------------- /Ch05_Arguments/listing5.11.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 5.11 3 | * Displaying a player's location 4 | */ 5 | 6 | var showPlayerPlace; 7 | 8 | showPlayerPlace = function (playerName, playerPlace) { 9 | console.log(playerName + " is in " + playerPlace); 10 | }; 11 | 12 | showPlayerPlace("Kandra", "The Dungeon of Doom"); 13 | showPlayerPlace("Dax", "The Old Library"); 14 | 15 | 16 | 17 | /* Further Adventures 18 | * 19 | * 1) Inside the console.log brackets, 20 | * change playerName to playerName[0] 21 | * Run the program. 22 | * What effect do the brackets have? 23 | * 24 | * 2) Change the number in the brackets to 1. 25 | * 26 | * 3) What happens when you change the 27 | * number to 3? Why? 28 | * 29 | */ 30 | -------------------------------------------------------------------------------- /Ch06_Return_values/listing6.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 6.01 3 | * Returning a value from a function 4 | */ 5 | 6 | var getMessage; 7 | var response; 8 | 9 | getMessage = function () { 10 | return "I’m going on an adventure!"; 11 | }; 12 | 13 | response = getMessage(); 14 | 15 | console.log(response); 16 | 17 | 18 | 19 | /* Further Adventures 20 | * 21 | * 1) Write a getMyMessage function 22 | * that returns a message of 23 | * your choosing. 24 | * 25 | * We can call functions at the console prompt. 26 | * Run the program, then click in the Console panel 27 | * and enter commands after the prompt, > 28 | * 29 | * 2) Call getMessage at the prompt. 30 | * 31 | * > getMessage() 32 | * 33 | * When a function is called at the prompt, 34 | * its return value is displayed. 35 | * 36 | * 3) Call getMyMessage at the prompt. 37 | * 38 | */ -------------------------------------------------------------------------------- /Ch06_Return_values/listing6.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 6.02 3 | * Using an argument to determine the return value 4 | */ 5 | 6 | var getHelloTo; 7 | var fullMessage; 8 | 9 | getHelloTo = function (name) { 10 | return "Hello to " + name; 11 | }; 12 | 13 | fullMessage = getHelloTo("Kandra"); 14 | 15 | console.log(fullMessage); 16 | 17 | 18 | 19 | /* Further Adventures 20 | * 21 | * 1) Change the definition of getHelloTo 22 | * to accept two parameters, name1 and name2. 23 | * 24 | * 2) Make the function return a string of 25 | * the form "Hello to Kandra and Dax" 26 | * 27 | */ 28 | -------------------------------------------------------------------------------- /Ch06_Return_values/listing6.03.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 6.03 3 | * Using the return value as an argument 4 | */ 5 | 6 | var getHelloTo; 7 | 8 | getHelloTo = function (name) { 9 | return "Hello to " + name; 10 | }; 11 | 12 | console.log(getHelloTo("Kandra")); 13 | console.log(getHelloTo("Dax")); 14 | 15 | 16 | 17 | /* Further Adventures 18 | * 19 | * The JavaScript replace function will 20 | * replace one string with another. 21 | * e.g. "Hello Bob".replace("Bob", "Babs"); 22 | * returns the string "Hello Babs" 23 | * 24 | * 1) Declare a variable called template 25 | * in the function body of getHelloTo. 26 | * i.e. var template; 27 | * 28 | * 2) Assign it the value "Hello to {{name}}" 29 | * 30 | * 3) Replace the {{name}} placeholder 31 | * with the value passed in as name. i.e. 32 | * template = template.replace("{{name}}", name); 33 | * 34 | * 4) Return template from the function. 35 | * i.e. return template; 36 | * 37 | * 5) Run the function. 38 | * Does it still work? 39 | * 40 | */ 41 | -------------------------------------------------------------------------------- /Ch06_Return_values/listing6.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 6.04 3 | * Returning the sum of two numbers 4 | */ 5 | 6 | var add; 7 | 8 | add = function (number1, number2) { 9 | var total = number1 + number2; 10 | 11 | return total; 12 | }; 13 | 14 | var sum = add(50, 23); 15 | 16 | console.log(sum); 17 | 18 | 19 | 20 | /* Further Adventures 21 | * 22 | * 1) Find and display the sum of 23 | * a different pair of numbers 24 | * 25 | * 2) Change the call to console.log so that 26 | * the display on the console reads: 27 | * 'The sum of 50 and 23 is 73' 28 | * using the add function to generate the answer. 29 | * 30 | * 3) Can you use the add function as it is 31 | * to add more than two numbers? 32 | * Hint: You can nest calls to add. 33 | * 34 | * 4) Create a function to return the sum of 35 | * three numbers given as arguments. 36 | * 37 | */ -------------------------------------------------------------------------------- /Ch06_Return_values/listing6.05.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 6.05 3 | * A function with three arguments 4 | */ 5 | 6 | var totalCost; 7 | 8 | totalCost = function (callOutCharge, costPerHour, numberOfHours) { 9 | return callOutCharge + costPerHour * numberOfHours; 10 | }; 11 | 12 | console.log("$" + totalCost(30, 40, 3)); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) What is the cost for 12 hours work? 19 | * 20 | * 2) The plumber sometimes issues discount coupons. 21 | * Add a fourth parameter to the totalCost 22 | * function definition to account for discounts. 23 | * 24 | * 3) Update the function to subtract the discount 25 | * from the total before returning it. 26 | * 27 | * 4) If a customer has a $20 off coupon, 28 | * update the call to totalCost 29 | * to display the total cost. 30 | * It should now be $130 for 3 hours. 31 | * 32 | */ -------------------------------------------------------------------------------- /Ch06_Return_values/listing6.07.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 6.07 3 | * Getting a string for a player’s name 4 | */ 5 | 6 | var getPlayerName; 7 | 8 | getPlayerName = function (playerName) { 9 | return playerName; 10 | }; 11 | 12 | console.log(getPlayerName("Kandra")); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Update the getPlayerName function 19 | * so it adds a prefix and suffix 20 | * made up of the = character. 21 | * The length of the prefix and suffix 22 | * should be 4. 23 | * 24 | * getPlayerName("Kiki") should return 25 | * ==== Kiki ==== 26 | * 27 | * getPlayerName("Mahesha") should return 28 | * ==== Mahesha ==== 29 | * 30 | * 2) Update the getPlayerName function 31 | * so it includes a border on the left 32 | * when displaying the name. 33 | * 34 | * getPlayerName("Jahver") should return 35 | * 36 | * | 37 | * | Jahver 38 | * | 39 | * 40 | * Note: to add a new-line character to a 41 | * string, use the escape sequence '\n' 42 | * 43 | * var msg = "\na\nb\n"; 44 | * console.log(msg) would display: 45 | * " 46 | * a 47 | * b 48 | * " 49 | * 50 | */ -------------------------------------------------------------------------------- /Ch06_Return_values/listing6.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 6.08 3 | * Getting strings for a player’s health and location 4 | */ 5 | 6 | var getPlayerHealth; 7 | var getPlayerPlace; 8 | 9 | getPlayerHealth = function (playerName, playerHealth) { 10 | return playerName + " has health " + playerHealth; 11 | }; 12 | 13 | getPlayerPlace = function (playerName, playerPlace) { 14 | return playerName + " is in " + playerPlace; 15 | }; 16 | 17 | console.log(getPlayerHealth("Kandra", 50)); 18 | console.log(getPlayerPlace("Kandra", "The Dungeon of Doom")); 19 | 20 | 21 | 22 | /* Further Adventures 23 | * 24 | * 1) Create a showPlayerInfo function with three 25 | * parameters, playerName, playerHealth and 26 | * playerPlace. 27 | * 28 | * 2) Call getPlayerHealth and getPlayerPlace 29 | * from your new showPlayerInfo function. 30 | * 31 | * e.g. showPlayerInfo("Dax", 50, "The Bath") 32 | * should produce the output 33 | * 34 | * Dax has health 50 35 | * Dax is in The Bath 36 | * 37 | */ -------------------------------------------------------------------------------- /Ch07_Object_arguments/listing7.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 7.01 3 | * Passing a function an object as an argument 4 | */ 5 | 6 | var planet1; 7 | var getPlanetInfo; 8 | 9 | planet1 = { 10 | name: "Jupiter", 11 | position: 5, 12 | type: "Gas Giant", 13 | radius: 69911, 14 | sizeRank: 1 15 | }; 16 | 17 | getPlanetInfo = function (planet) { 18 | return planet.name + ": planet number " + planet.position; 19 | }; 20 | 21 | console.log(getPlanetInfo(planet1)); 22 | 23 | 24 | 25 | /* Further Adventures 26 | * 27 | * 1) Create a second planet object. 28 | * 29 | * 2) Use getPlanetInfo to log details of 30 | * the second planet. 31 | * 32 | * 3) Update the getPlanetInfo function to 33 | * include more information about 34 | * each planet. 35 | * 36 | */ -------------------------------------------------------------------------------- /Ch07_Object_arguments/listing7.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 7.02 3 | * A function that adds properties to an object 4 | */ 5 | 6 | var planet1 = { name: "Jupiter", radius: 69911 }; 7 | 8 | var calculateSizes = function (planet) { 9 | var r = planet.radius; 10 | planet.area = 4 * 3.142 * r * r; 11 | planet.volume = 4 * 3.142 * r * r * r / 3; 12 | }; 13 | 14 | var displaySizes = function (planet) { 15 | console.log(planet.name); 16 | console.log("surface area = " + planet.area + " square km"); 17 | console.log("volume = " + planet.volume + " cubic km"); 18 | }; 19 | 20 | calculateSizes(planet1); 21 | displaySizes(planet1); 22 | 23 | 24 | 25 | /* Further Adventures 26 | * 27 | * The diameter of a planet is double its radius. 28 | * 29 | * 1) Update the calculateSizes function so it also 30 | * adds a diameter property to the planet. 31 | * 32 | * 2) Update the displaySizes function so it also 33 | * displays the diameter on the console. 34 | * 35 | */ -------------------------------------------------------------------------------- /Ch07_Object_arguments/listing7.03.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 7.03 3 | * A function to create planets 4 | */ 5 | 6 | var buildPlanet; 7 | var getPlanetInfo; 8 | var planet1; 9 | var planet2; 10 | 11 | buildPlanet = function (name, position, type, radius, rank) { 12 | return { 13 | name: name, 14 | position: position, 15 | type: type, 16 | radius: radius, 17 | sizeRank: rank 18 | }; 19 | }; 20 | 21 | getPlanetInfo = function (planet) { 22 | return planet.name.toUpperCase() + ": planet " + planet.position; 23 | }; 24 | 25 | planet1 = buildPlanet("Jupiter", 5, "Gas Giant", 69911, 1); 26 | planet2 = buildPlanet("Neptune", 8, "Ice Giant", 24622, 4); 27 | 28 | console.log(getPlanetInfo(planet1)); 29 | console.log(getPlanetInfo(planet2)); 30 | 31 | 32 | 33 | /* Further Adventures 34 | * 35 | * 1) Write a similar program to create and 36 | * display objects that represent cars. 37 | * 38 | */ -------------------------------------------------------------------------------- /Ch07_Object_arguments/listing7.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 7.04 3 | * Moving a point in 2D 4 | */ 5 | 6 | var point1; 7 | var point2; 8 | var move; 9 | var showPoint; 10 | 11 | move = function (point, change) { 12 | return { 13 | x: point.x + change.x, 14 | y: point.y + change.y 15 | }; 16 | }; 17 | 18 | showPoint = function (point) { 19 | console.log("( " + point.x + " , " + point.y + " )"); 20 | }; 21 | 22 | point1 = { x : 2, y : 5 }; 23 | 24 | point2 = move(point1, { x : 4, y : -2 }); 25 | 26 | showPoint(point1); 27 | console.log("Move 4 across and 2 down"); 28 | showPoint(point2); 29 | 30 | 31 | 32 | /* Further Adventures 33 | * 34 | * 1) Change the amount by which the point is moved 35 | * by altering the x and y properties of 36 | * the object literal passed to the move function. 37 | * 38 | * 2) Write a reflectX function that reflects 39 | * a point in the x-axis, returning the new point. 40 | * 41 | * 3) How about a rotate90 function that rotates 42 | * the point by 90 degrees anticlockwise 43 | * around ( 0 , 0 )? 44 | * 45 | */ -------------------------------------------------------------------------------- /Ch07_Object_arguments/listing7.05.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 7.05 3 | * Using Math.min and Math.max 4 | */ 5 | 6 | var showSmaller = function (num1, num2) { 7 | var smaller = Math.min(num1, num2); 8 | var larger = Math.max(num1, num2); 9 | 10 | console.log(smaller + " is smaller than " + larger); 11 | }; 12 | 13 | showSmaller(12, 3); 14 | showSmaller(-10, 3); 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Try out some different arguments. 21 | * 22 | * 2) Write a showLarger function. 23 | * 24 | * 3) Write a showSmallest function that takes 25 | * three arguments and displays the smallest 26 | * of the three. 27 | * 28 | */ -------------------------------------------------------------------------------- /Ch07_Object_arguments/listing7.06.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 7.06 3 | * Using Math.min and Math.max to constrain an argument 4 | */ 5 | 6 | var line = function (lineLength) { 7 | var line = "========================================"; 8 | lineLength = Math.max(0, lineLength); 9 | lineLength = Math.min(40, lineLength); 10 | return line.substr(0, lineLength); 11 | }; 12 | 13 | console.log(line(30)); 14 | console.log(line(40)); 15 | console.log(line(50)); 16 | 17 | 18 | 19 | /* Further Adventures 20 | * 21 | * 1) Test line lengths from -20 to 60 22 | * in steps of 10. 23 | * 24 | * 2) Define a spaces function that returns a string 25 | * made up of a specified number of space characters. 26 | * The line of spaces returned can have a length 27 | * between 0 and 40. 28 | * 29 | * 3) Add an emptyBox function 30 | * that draws an empty box of specified 31 | * width and height 5. 32 | * 33 | * emptyBox(12); 34 | * 35 | * > ============ 36 | * > = = 37 | * > = = 38 | * > = = 39 | * > ============ 40 | * 41 | */ -------------------------------------------------------------------------------- /Ch07_Object_arguments/listing7.07.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 7.07 3 | * Converting a string to upper case 4 | */ 5 | 6 | var planet = "Jupiter"; 7 | var bigPlanet = planet.toUpperCase(); 8 | 9 | console.log(planet + " becomes " + bigPlanet); 10 | 11 | 12 | 13 | /* Further Adventures 14 | * 15 | * 1) Create a getBig function that accepts a 16 | * string as an argument and returns it 17 | * converted to upper case. 18 | * 19 | * There is also a toLowerCase string method. 20 | * 21 | * 2) Create a getSmall function that accepts a 22 | * string as an argument and returns it 23 | * converted to lower case. 24 | * 25 | */ -------------------------------------------------------------------------------- /Ch07_Object_arguments/listing7.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 7.08 3 | * Finding substrings 4 | */ 5 | 6 | var message = "We choose to go to the Moon!"; 7 | 8 | console.log(message.substr(3, 12)); 9 | 10 | 11 | 12 | /* Further Adventures 13 | * 14 | * 1) Use substr to log the following parts of 15 | * the message string to the console: 16 | * 17 | * > Moon! 18 | * > go to the 19 | * > We choose 20 | * 21 | * 2) Investigate what happens if you omit the 22 | * second argument when calling substr. 23 | * 24 | * 3) What happens if you use negative numbers 25 | * as arguments? 26 | * 27 | */ -------------------------------------------------------------------------------- /Ch07_Object_arguments/listing7.09.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 7.09 3 | * Finding a character with indexOf 4 | */ 5 | 6 | var message = "We choose to go to the Moon!"; 7 | 8 | var charIndex = message.indexOf("M"); 9 | 10 | console.log(message.substr(charIndex, 3)); 11 | 12 | 13 | 14 | /* Further Adventures 15 | * 16 | * 1) Use indexOf to find the index of where the 17 | * word 'go' starts in the message string. 18 | * 19 | * 2) Use indexOf and substr to display the word 20 | * 'choose' from the message string. 21 | * 22 | * There is also a lastIndexOf string method. 23 | * 24 | * 3) Use lastIndexOf to find the index of the 25 | * 'oo' in the word 'Moon' of the message string. 26 | * 27 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.01 3 | * Creating arrays 4 | */ 5 | 6 | var scores; 7 | var names; 8 | 9 | scores = [ 3, 1, 8, 2 ]; 10 | names = [ "Kandra", "Dax", "Blinky" ]; 11 | 12 | console.log(scores); 13 | console.log(names); 14 | 15 | 16 | 17 | /* Further Adventures 18 | * 19 | * 1) Add an extra element to each array. 20 | * 21 | * 2) Declare a third variable, akaScores. 22 | * 23 | * 3) Assign the scores array to akaScores. 24 | * akaScores = scores 25 | * 26 | * 4) Log akaScores to the console. 27 | * 28 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.02 3 | * Using existing objects in an array 4 | */ 5 | 6 | var place1 = { name : "The Pyramids", country : "Egypt" }; 7 | var place2 = { name : "The Grand Canyon", country : "USA" }; 8 | var place3 = { name : "Bondi Beach", country : "Australia" }; 9 | 10 | var thisYear = [ place1, place2 ]; 11 | var nextYear = [ place3 ]; 12 | 13 | console.log(thisYear); 14 | console.log(nextYear); 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Add place1 to the nextYear array. 21 | * Can it be in two arrays at once? 22 | * 23 | * 2) Run the program again to check if an 24 | * object can be in two arrays at once. 25 | * 26 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.03.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.03 3 | * Accessing array elements 4 | */ 5 | 6 | var scores = [ 3, 1, 8, 2 ]; 7 | 8 | console.log("There are " + scores.length + " scores:"); 9 | console.log("The first score is " + scores[0]); 10 | console.log("The second score is " + scores[1]); 11 | console.log("The third score is " + scores[2]); 12 | console.log("The fourth score is " + scores[3]); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Add a fifth score to the array. 19 | * 20 | * 2) Add an extra console.log to display the new score. 21 | * 22 | * The index of the last element is one less than 23 | * the length of the array. 24 | * 25 | * 3) Log the value of the last element to the console. 26 | * Start the message with "The last score is " and 27 | * concatenate the score to the string. 28 | * Use an expression involving scores.length 29 | * as the index for the last element. 30 | * 31 | * 4) Add an extra score to the array and 32 | * run the program again. 33 | * 34 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.04 3 | * Using a variable as an index 4 | */ 5 | 6 | var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]; 7 | var dayInWeek = 4; 8 | 9 | console.log( days[dayInWeek] ); 10 | console.log( days[dayInWeek - 1] ); 11 | 12 | 13 | 14 | /* Further Adventures 15 | * 16 | * 1) Change the value of the dayInWeek variable 17 | * to display a different day. 18 | * 19 | * 2) Write a function that accepts a number for the 20 | * day of the week to be displayed and returns 21 | * the day as a string. Assign the function to 22 | * a getDay variable. 23 | * 24 | * 3) Call your function, passing it 4 as the day 25 | * of the week, and log the value it returns. 26 | * 27 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.06.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.06 3 | * Manipulating arrays with push, pop and join 4 | */ 5 | 6 | var items = []; 7 | var item = "The Pyramids"; 8 | var removed; 9 | 10 | items.push(item); 11 | items.push("The Grand Canyon"); 12 | items.push("Bondi Beach"); 13 | 14 | console.log(items); 15 | 16 | removed = items.pop(); 17 | 18 | console.log(removed + " was removed"); 19 | console.log(items.join(" and ")); 20 | 21 | 22 | 23 | /* Further Adventures 24 | * 25 | * 1) Push another item onto the array. 26 | * 27 | * 2) Log the joined items. 28 | * 29 | * You can set an array element by using its index. 30 | * items[2] = "The Taj Mahal" 31 | * 32 | * 3) Set one of the items using square brackets. 33 | * 34 | * 4) Can you push more than one item at a time? 35 | * 36 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.07.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.07 3 | * Iterating over an array with forEach 4 | */ 5 | 6 | var items; 7 | var showInfo; 8 | 9 | items = [ 10 | "The Pyramids", 11 | "The Grand Canyon", 12 | "Bondi Beach" 13 | ]; 14 | 15 | showInfo = function (itemToShow) { 16 | console.log(itemToShow); 17 | }; 18 | 19 | items.forEach(showInfo); 20 | 21 | 22 | 23 | /* Further Adventures 24 | * 25 | * 1) Add a few extra items to the array, some 26 | * using push and some using square brackets. 27 | * Run the program. 28 | * 29 | * Strings also have a length property 30 | * which gives the number of characters in the string. 31 | * 32 | * 2) Update the showInfo function to also 33 | * display the number of letters in each item. 34 | * 35 | * 3) Write a new function that finds the total 36 | * number of letters of the elements in 37 | * the items array. 38 | * 39 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.08 3 | * Calling forEach with an inline function 4 | */ 5 | 6 | var items = [ "The Pyramids", "The Grand Canyon", "Bondi Beach" ]; 7 | 8 | console.log("Dream destinations:"); 9 | 10 | items.forEach(function (item) { 11 | console.log(" – " + item); 12 | }); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Change the program to show the number of 19 | * places to visit as well as the places themselves. 20 | * 21 | * 2) Wrap the display code in a function 22 | * and assign it to the showItems variable. 23 | * 24 | * 3) Click after the prompt on the console, 25 | * type showItems() and press Enter. 26 | * Can you run your function from the console? 27 | * 28 | * 4) Add and remove array items via the console 29 | * and display the current state of the array 30 | * with the showItems function. 31 | * 32 | * You can press the up arrow when working at the 33 | * console prompt to show previous commands used. 34 | * 35 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.09.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.09 3 | * Using the arguments passed by forEach 4 | */ 5 | 6 | var players; 7 | var showArguments; 8 | 9 | players = [ "Dax", "Jahver", "Kandra" ]; 10 | 11 | showArguments = function (item, index, wholeArray) { 12 | console.log("Item: " + item); 13 | console.log("Index: " + index); 14 | console.log("Array: " + wholeArray); 15 | }; 16 | 17 | players.forEach(showArguments); 18 | 19 | 20 | /* Further Adventures 21 | * 22 | * 1) Add two more names to the players array. 23 | * 24 | * 2) Run the program. 25 | * 26 | * 3) Change the first console.log so 27 | * it uses wholeArray and index 28 | * to display the current item in 29 | * the array. Don't use the item 30 | * parameter. 31 | * 32 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.10.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.10 3 | * Using the arguments passed by forEach - compact 4 | */ 5 | 6 | [ "Dax", "Jahver", "Kandra" ].forEach(function (item, index, wholeArray) { 7 | console.log("Item: " + item); 8 | console.log("Index: " + index); 9 | console.log("Array: " + wholeArray); 10 | }); 11 | 12 | 13 | 14 | /* Further Adventures 15 | * 16 | * 1) Create an array of rectangle objects. 17 | * Each rectangle should have a length 18 | * property and a width property. 19 | * 20 | * 2) Define an assignArea function that 21 | * takes a rectangle as an argument and 22 | * assigns an area property holding 23 | * the area of the rectangle. 24 | * 25 | * 3) Define a showInfo function that 26 | * takes a rectangle as an argument and 27 | * displays its properties on the console. 28 | * 29 | * 4) Use forEach and your two functions 30 | * to display info about each of 31 | * the rectangles in the array. 32 | * 33 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.11.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.11 3 | * Finding the total shopping bill 4 | */ 5 | 6 | var getTotalBill = function (itemCosts, itemCounts) { 7 | var total = 0; 8 | 9 | itemCosts.forEach(function (cost, i) { 10 | total += cost * itemCounts[i]; 11 | }); 12 | 13 | return total; 14 | }; 15 | 16 | var costs = [ 1.99, 4.95, 2.50, 9.87 ]; 17 | var numOfEach = [ 2, 1, 5, 2 ]; 18 | 19 | console.log("The total cost is $" + getTotalBill(costs, numOfEach)); 20 | 21 | 22 | 23 | /* Further Adventures 24 | * 25 | * 1) Add an extra item to the shopping trip. 26 | * 27 | * 2) Change the function so that forEach iterates 28 | * over itemCounts instead of itemCosts. 29 | * Can you make it work? 30 | * 31 | * 3) Create a single array of objects with 32 | * cost and numberBought properties. 33 | * 34 | * items = [ { cost: 1.99, numberBought: 2 }, 35 | * { cost: 4.95, numberBought: 1 } ]; 36 | * 37 | * 4) Update getTotalBill to accept a single array 38 | * of items. 39 | * 40 | */ -------------------------------------------------------------------------------- /Ch08_Arrays/listing8.12.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 8.12 3 | * Displaying a multiple choice question 4 | */ 5 | 6 | var displayQuestion = function (questionAndAnswer) { 7 | var options = [ "A", "B", "C", "D", "E" ]; 8 | 9 | console.log(questionAndAnswer.question); 10 | 11 | questionAndAnswer.answers.forEach( 12 | function (answer, i) { 13 | console.log(options[i] + " - " + answer); 14 | } 15 | ); 16 | }; 17 | 18 | var question1 = { 19 | question : "What is the capital of France?", 20 | answers : [ 21 | "Bordeaux", 22 | "F", 23 | "Paris", 24 | "Brussels" 25 | ], 26 | correctAnswer : "Paris" 27 | }; 28 | 29 | displayQuestion(question1); 30 | 31 | 32 | 33 | /* Further Adventures 34 | * 35 | * 1) Add two more question objects. 36 | * 37 | * 2) Create an array with your question objects. 38 | * 39 | * 3) Use forEach to call displayQuestion on 40 | * each of your questions. 41 | * 42 | * 4) Can you execute a command at the prompt 43 | * to display a particular question? 44 | */ -------------------------------------------------------------------------------- /Ch09_Constructors/listing9.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 9.01 3 | * Using a function to create an object 4 | */ 5 | 6 | var buildPlanet = function (name, position, type) { 7 | var planet = {}; 8 | 9 | planet.name = name; 10 | planet.position = position; 11 | planet.type = type; 12 | 13 | return planet; 14 | }; 15 | 16 | var planet1 = buildPlanet( 17 | "Jupiter", 18 | 5, 19 | "Gas Giant" 20 | ); 21 | 22 | console.log(planet1.name); 23 | console.log(planet1.position); 24 | console.log(planet1.type); 25 | 26 | 27 | 28 | /* Further Adventures 29 | * 30 | * 1) Build a second planet, 31 | * using the buildPlanet function. 32 | * 33 | * 2) Log its name and type. 34 | * 35 | */ -------------------------------------------------------------------------------- /Ch09_Constructors/listing9.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 9.02 3 | * Adding methods to our constructed object 4 | */ 5 | 6 | var buildPlanet = function (name, position, type) { 7 | var planet = {}; 8 | 9 | planet.name = name; 10 | planet.position = position; 11 | planet.type = type; 12 | 13 | planet.showPlanet = function () { 14 | var info = planet.name; 15 | info += ": planet " + planet.position; 16 | info += " - " + planet.type; 17 | console.log(info); 18 | }; 19 | 20 | return planet; 21 | }; 22 | 23 | var planet1 = buildPlanet( 24 | "Jupiter", 25 | 5, 26 | "Gas Giant" 27 | ); 28 | 29 | planet1.showPlanet(); 30 | 31 | 32 | 33 | /* Further Adventures 34 | * 35 | * 1) Create a second planet. 36 | * Use the buildPlanet function. 37 | * 38 | * 2) Call the showPlanet method on your 39 | * new planet. 40 | * 41 | */ -------------------------------------------------------------------------------- /Ch09_Constructors/listing9.03.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 9.03 3 | * An array of constructed objects 4 | */ 5 | 6 | var buildPlanet = function (name, position, type) { 7 | var planet = {}; 8 | 9 | planet.name = name; 10 | planet.position = position; 11 | planet.type = type; 12 | 13 | planet.showPlanet = function () { 14 | var info = planet.name; 15 | info += ": planet " + planet.position; 16 | info += " - " + planet.type; 17 | console.log(info); 18 | }; 19 | 20 | return planet; 21 | }; 22 | 23 | var planets = [ 24 | buildPlanet( "Jupiter", 5, "Gas Giant" ), 25 | buildPlanet( "Neptune", 8, "Ice Giant" ), 26 | buildPlanet( "Mercury", 1, "Terrestrial" ) 27 | ]; 28 | 29 | planets.forEach(function (planet) { 30 | planet.showPlanet(); 31 | }); 32 | 33 | 34 | 35 | /* Further Adventures 36 | * 37 | * 1) Add two more planets to the planets array. 38 | * 39 | * 2) Run the program to display them. 40 | * 41 | * 3) Add code to visually separate each planet 42 | * from the next. 43 | * 44 | */ -------------------------------------------------------------------------------- /Ch09_Constructors/listing9.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 9.04 3 | * A Planet constructor 4 | */ 5 | 6 | var Planet = function (name, position, type) { 7 | this.name = name; 8 | this.position = position; 9 | this.type = type; 10 | 11 | this.showPlanet = function () { 12 | var info = this.name + ": planet " + this.position; 13 | info += " - " + this.type; 14 | console.log(info); 15 | }; 16 | }; 17 | 18 | var planet = new Planet( "Jupiter", 5, "Gas Giant" ); 19 | 20 | planet.showPlanet(); 21 | 22 | 23 | 24 | /* Further Adventures 25 | * 26 | * 1) Use the Planet constructor function 27 | * to create a second planet. 28 | * Don't forget the 'new' keyword. 29 | * 30 | * 2) Call the showPlanet method on your 31 | * newly created planet. 32 | * 33 | */ -------------------------------------------------------------------------------- /Ch09_Constructors/listing9.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 9.08 3 | * A calendar event constructor 4 | */ 5 | 6 | var CalendarEvent = function (title, startDate, startTime, endTime) { 7 | this.title = title; 8 | this.startDate = startDate; 9 | this.startTime = startTime; 10 | this.endTime = endTime; 11 | 12 | this.showEvent = function () { 13 | var dateString = [ 14 | this.startDate, 15 | ", from ", 16 | this.startTime, 17 | " to ", 18 | this.endTime 19 | ].join(""); 20 | 21 | console.log(this.title); 22 | console.log(dateString); 23 | }; 24 | }; 25 | 26 | var calEvent = new CalendarEvent( 27 | "Annual Review", 28 | "3/5/16", 29 | "4.00pm", 30 | "5.00pm" 31 | ); 32 | 33 | calEvent.showEvent(); 34 | 35 | 36 | 37 | /* Further Adventures 38 | * 39 | * 1) Add a second event. 40 | * 41 | * 2) Call the showEvent method on your 42 | * new calendar event. 43 | * 44 | * 3) Update the showEvent method so that 45 | * its output looks like this: 46 | * Annual Review: 3/5/16 - (4.00pm - 5.00pm) 47 | * 48 | */ -------------------------------------------------------------------------------- /Ch09_Constructors/listing9.09.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 9.09 3 | * A Place constructor part 1 4 | */ 5 | 6 | var Place = function (title, description) { 7 | this.title = title; 8 | this.description = description; 9 | 10 | this.getInfo = function () { 11 | var infoString = this.title + "\n"; 12 | infoString += this.description + "\n"; 13 | return infoString; 14 | }; 15 | }; 16 | 17 | var library = new Place( 18 | "The Old Library", 19 | "You are in a library. Dusty books line the walls." 20 | ); 21 | 22 | console.log(library.getInfo()); 23 | 24 | 25 | 26 | /* Further Adventures 27 | * 28 | * 1) Add a second place. 29 | * 30 | * 2) Call the getInfo method on your 31 | * new place. 32 | * 33 | */ -------------------------------------------------------------------------------- /Ch10_Bracket_notation/listing10.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 10.01 3 | * Bracket notation for object properties 4 | */ 5 | 6 | var ages = {}; 7 | 8 | ages["Kandra"] = 56; 9 | ages["Dax"] = 21; 10 | 11 | console.log(ages.Kandra); 12 | console.log(ages.Dax); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Add a couple more properties to 19 | * the ages object. 20 | * 21 | * 2) Change the calls to console.log 22 | * to use square bracket notation as well. 23 | * 24 | */ -------------------------------------------------------------------------------- /Ch10_Bracket_notation/listing10.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 10.02 3 | * Longer strings as keys 4 | */ 5 | 6 | var ages = {}; 7 | 8 | ages["Kandra Smith"] = 56; 9 | ages["Dax Aniaku"] = 21; 10 | 11 | console.log(ages["Kandra Smith"]); 12 | console.log(ages["Dax Aniaku"]); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Add a couple more properties to the ages object. 19 | * Include spaces and other characters 20 | * in the keys. 21 | * 22 | * 2) Log the values to the console. 23 | * 24 | * 3) Try changing the properties so they 25 | * use dot notation. What happens? 26 | * 27 | */ -------------------------------------------------------------------------------- /Ch10_Bracket_notation/listing10.03.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 10.03 3 | * Using a function to add ages 4 | */ 5 | 6 | var ages = {}; 7 | 8 | var addAge = function (name, age) { 9 | ages[name] = age; 10 | }; 11 | 12 | addAge("Kandra Smith", 56); 13 | addAge("Dax Aniaku", 21); 14 | 15 | console.log(ages["Kandra Smith"]); 16 | console.log(ages["Dax Aniaku"]); 17 | 18 | 19 | 20 | /* Further Adventures 21 | * 22 | * 1) Use the addAge function to add a couple 23 | * more people and their ages. 24 | * 25 | * 2) Log the new info to the console. 26 | * 27 | * 3) At the console prompt, 28 | * type in 'ages' and press Enter. 29 | * The ages object should be displayed. 30 | * 31 | * 4) Still at the console, 32 | * use the addAge function to add new info. 33 | * e.g. > addAge("Bob", 99); 34 | * 35 | * 5) Type 'ages' again to see the updated 36 | * ages object. 37 | * 38 | */ -------------------------------------------------------------------------------- /Ch10_Bracket_notation/listing10.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 10.04 3 | * Using Object.keys 4 | */ 5 | 6 | var ages = { 7 | "Kandra Smith" : 56, 8 | "Dax Aniaku" : 210, 9 | "Blinky" : 36 10 | }; 11 | 12 | var keys = Object.keys(ages); 13 | 14 | console.log(keys); 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Add a couple more key-value pairs 21 | * to the ages object. 22 | * 23 | * 2) Add another property to the ages object 24 | * using square bracket notation. 25 | * Add the code before the call to 26 | * Object.keys, so the new key will 27 | * be logged to the console. 28 | * 29 | * 3) At the console prompt, 30 | * access individual elements of the 31 | * keys array by using index notation. 32 | * e.g. > keys[2]; 33 | * 34 | */ -------------------------------------------------------------------------------- /Ch10_Bracket_notation/listing10.05.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 10.05 3 | * Iterating over Object.keys with forEach 4 | */ 5 | 6 | var ages = { 7 | "Kandra Smith" : 56, 8 | "Dax Aniaku" : 21, 9 | "Blinky" : 36 10 | }; 11 | 12 | var keys = Object.keys(ages); 13 | 14 | keys.forEach(function (key) { 15 | console.log(key); 16 | }); 17 | 18 | 19 | 20 | /* Further Adventures 21 | * 22 | * 1) Add three more people to the ages object: 23 | * One directly to the object, 24 | * one using square brackets and 25 | * one using dot notation. 26 | * 27 | * 2) Update the call to console.log so it 28 | * shows the age of each person. 29 | * Remember, the key lets you access 30 | * the value from the ages object. 31 | * 32 | * 3) Update the call to console.log so it 33 | * shows the person's name along with 34 | * the length of their name. 35 | * e.g. Blinky: 6 36 | 37 | */ -------------------------------------------------------------------------------- /Ch10_Bracket_notation/listing10.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 10.08 3 | * An exits object 4 | */ 5 | 6 | var Place = function (title) { 7 | this.title = title; 8 | }; 9 | 10 | var kitchen = new Place("The Kitchen"); 11 | var dungeon = new Place("The Dungeon"); 12 | 13 | var exits = {}; 14 | 15 | exits["north"] = kitchen; 16 | exits["the trapdoor"] = dungeon; 17 | 18 | var keys = Object.keys(exits); 19 | 20 | keys.forEach(function (key) { 21 | console.log(key + " goes to " + exits[key].title); 22 | }); 23 | 24 | 25 | 26 | /* Further Adventures 27 | * 28 | * 1) Add a couple more places. 29 | * 30 | * 2) Add a couple more exits. 31 | * 32 | * 3) At the console prompt, 33 | * type 'exits' to see the exits object. 34 | * It has a number of keys with the 35 | * corresponding values being place objects. 36 | * 37 | * 4) Access one of the exits like this: 38 | * > exits["north"]; 39 | * A single object will be displayed. 40 | * 41 | * 5) Access the title property: 42 | * > exits["north"].title; 43 | * 44 | * 6) Or use square brackets: 45 | * > exits["north"]["title"] 46 | * 47 | */ -------------------------------------------------------------------------------- /Ch11_Scope/listing11.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 11.01 3 | * Declare, assign and use a variable 4 | */ 5 | 6 | var mountain; 7 | 8 | mountain = "Ben Nevis"; 9 | 10 | console.log(mountain); 11 | 12 | 13 | /* Further Adventures 14 | * 15 | * Run the program, then at the console prompt: 16 | * 17 | * 1) Type mountain and press enter. 18 | * 19 | * The value assigned to the mountain variable 20 | * should be displayed. 21 | * 22 | * mountain is a global variable. 23 | * It can be accessed anywhere in the program. 24 | * 25 | * 2) Assign the mountain variable a new value. 26 | * e.g. mountain = "Everest" 27 | * 28 | * 3) Type mountain and press enter. 29 | * 30 | */ -------------------------------------------------------------------------------- /Ch11_Scope/listing11.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 11.02 3 | * Accessing a variable from within a function 4 | */ 5 | 6 | var mountain = "Ben Nevis"; 7 | 8 | var showMountain = function () { 9 | console.log(mountain); 10 | }; 11 | 12 | showMountain(); 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * The mountain and showMountain variables are 19 | * accessible anywhere in the program. 20 | * 21 | * 1) At the console prompt, 22 | * type 'showMountain' and press enter. 23 | * 24 | * The function definition is displayed. 25 | * (JS Bin may have changed it to use its 26 | * own log function) 27 | * 28 | * 2) Type 'showMountain()' and press enter. 29 | * 30 | * "Ben Nevis" is displayed, followed by the 31 | * return value of the function, undefined. 32 | * 33 | * 3) Change the definition of the showMountain 34 | * function to return the value of 35 | * the mountain variable. 36 | * i.e. return mountain; 37 | * 38 | * 4) Run the program and clear the console. 39 | * 40 | * 5) Type 'showMountain()' and press enter. 41 | * 42 | */ -------------------------------------------------------------------------------- /Ch11_Scope/listing11.03.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 11.03 3 | * A tiny quiz 4 | */ 5 | 6 | var question = "What is the highest mountain in Wales?"; 7 | var answer = "Snowdon"; 8 | 9 | console.log(question); 10 | 11 | 12 | 13 | /* Further Adventures 14 | * 15 | * 1) At the prompt, 16 | * type 'answer' and press enter. 17 | * 18 | * 2) Assign answer a new value. 19 | * 20 | * Both question and answer are accessible 21 | * anywhere in the program, including inside functions. 22 | * 23 | * 3) Define a showQandA function to display 24 | * the question and answer. 25 | * 26 | * 4) Run the program. 27 | * 28 | * 5) Call the showQandA function at the console prompt. 29 | * 30 | */ -------------------------------------------------------------------------------- /Ch11_Scope/listing11.05.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 11.05 3 | * The variable is visible within the function 4 | */ 5 | 6 | var show = function () { 7 | var mountain = "Devils Tower"; 8 | console.log(mountain); 9 | }; 10 | 11 | show(); 12 | 13 | 14 | /* Further Adventures 15 | * 16 | * 1) Declare another variable called mountain, 17 | * outside of the function, at the end 18 | * of the program. 19 | * e.g. var mountain = "Everest"; 20 | * 21 | * 2) Run the program. 22 | * 23 | * 3) At the prompt, type 'mountain' and press enter. 24 | * 25 | * Within the show function, the local version 26 | * of mountain is used. 27 | * 28 | * Outside the show function, the global version 29 | * of mountain is used. 30 | * 31 | */ -------------------------------------------------------------------------------- /Ch11_Scope/listing11.06.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 11.06 3 | * Global and local variables 4 | */ 5 | 6 | var mountain; 7 | var show; 8 | 9 | mountain = "Ben Nevis"; 10 | 11 | show = function () { 12 | var secretMountain = "Devils Tower"; 13 | 14 | console.log(mountain); 15 | console.log(secretMountain); 16 | }; 17 | 18 | show(); 19 | 20 | console.log(mountain); 21 | console.log(secretMountain); 22 | 23 | 24 | 25 | /* Further Adventures 26 | * 27 | * 1) At the prompt, assign mountain a new value. 28 | * 29 | * 2) Call show at the prompt. 30 | * 31 | */ -------------------------------------------------------------------------------- /Ch11_Scope/listing11.07.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 11.07 3 | * A counter 4 | */ 5 | 6 | var counter = 0; 7 | 8 | var count = function () { 9 | counter = counter + 1; 10 | return counter; 11 | }; 12 | 13 | 14 | 15 | /* Further Adventures 16 | * 17 | * 1) Type the following commands at the prompt: 18 | * 19 | * > count() 20 | * > count() 21 | * > count() 22 | * 23 | * The current value of counter is displayed on the 24 | * console for each call to count. 25 | * 26 | * 2) Type the following commands at the prompt: 27 | * 28 | * > counter = 99 29 | * > count() 30 | * > count() 31 | * 32 | * Because counter is a global variable, you can 33 | * change it at the console. 34 | * 35 | */ -------------------------------------------------------------------------------- /Ch11_Scope/listing11.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 11.08 3 | * Hiding the counter variable 4 | */ 5 | 6 | var getCounter = function () { 7 | var counter = 0; 8 | 9 | var countUpBy1 = function () { 10 | counter = counter + 1; 11 | return counter; 12 | }; 13 | 14 | return countUpBy1; 15 | }; 16 | 17 | var count = getCounter(); 18 | 19 | 20 | 21 | /* Further Adventures 22 | * 23 | * 1) At the prompt, type the following commands: 24 | * 25 | * > count() 26 | * > count() 27 | * > count() 28 | * 29 | * Each time count is called, the current value of 30 | * counter is displayed on the console. 31 | * 32 | * 2) At the prompt, type the following commands: 33 | * 34 | * > counter 35 | * 36 | * The counter variable is local to the getCounter 37 | * function; that's where it was declared. It can't 38 | * be accessed from the console because it is not 39 | * a global variable. 40 | * 41 | */ -------------------------------------------------------------------------------- /Ch11_Scope/listing11.09.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 11.09 3 | * Multiple counters 4 | */ 5 | 6 | var getCounter = function () { 7 | var counter = 0; 8 | 9 | return function () { 10 | counter = counter + 1; 11 | return counter; 12 | }; 13 | }; 14 | 15 | var peakCount = getCounter(); 16 | var climbCount = getCounter(); 17 | 18 | 19 | 20 | /* Further Adventures 21 | * 22 | * 1) At the prompt, type these commands: 23 | * 24 | * > peakCount() 25 | * > peakCount() 26 | * > peakCount() 27 | * > climbCount() 28 | * > peakCount() 29 | * 30 | * Each count function gets its own counter variable. 31 | * They do not interfere with each other. 32 | * 33 | * 2) Call getCounter a third time and assign the 34 | * function returned to a new variable. 35 | * 36 | * 3) Use the new variable to count up. 37 | * 38 | */ -------------------------------------------------------------------------------- /Ch11_Scope/listing11.10.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 11.10 3 | * A Counter constructor 4 | */ 5 | 6 | var Counter = function () { 7 | var counter = 0; 8 | 9 | this.count = function () { 10 | counter = counter + 1; 11 | return counter; 12 | }; 13 | }; 14 | 15 | var peaks = new Counter(); 16 | var climbs = new Counter(); 17 | 18 | 19 | 20 | /* Further Adventures 21 | * 22 | * 1) At the console prompt, 23 | * call peaks.count and climbs.count 24 | * a number of times. 25 | * 26 | * 2) Add a countDown method to the 27 | * Counter constructor. 28 | * The method should decrease the counter 29 | * by 1 each time it is called. 30 | * 31 | * 3) Add a countUpBy method that counts up 32 | * by a specified amont. 33 | * e.g. 34 | * > peaks.count() 35 | * 1 36 | * > peaks.countUpBy(5) 37 | * 6 38 | * > peaks.countDown() 39 | * 5 40 | * 41 | */ -------------------------------------------------------------------------------- /Ch12_Conditions/listing12.01.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 12.01 3 | * Guess the number 4 | */ 5 | 6 | var secret = 8; 7 | 8 | var guess = function (userNumber) { 9 | if (userNumber === secret) { 10 | console.log("Well done!"); 11 | } 12 | }; 13 | 14 | 15 | 16 | /* Further Adventures 17 | * 18 | * 1) Click run. 19 | * 20 | * 2) Call the guess function at the prompt. 21 | * e.g. guess(2) 22 | * 23 | * 3) Try a few different numbers as arguments. 24 | * 25 | * The program only prints "Well done!" when the 26 | * number you guess matches the secret number. 27 | * 28 | * The strict equality operator, ===, checks if 29 | * two values are equal. 30 | * 31 | * 4) Change the condition in parentheses to now 32 | * check if the guess is greater than the secret. 33 | * 34 | * if (userNumber > secret) 35 | * 36 | * 5) Change the message logged to "Too high!" 37 | * 38 | * 6) Run and test the updated program. 39 | * 40 | */ -------------------------------------------------------------------------------- /Ch12_Conditions/listing12.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 12.02 3 | * Guess the number - the else clause 4 | */ 5 | 6 | var secret = 8; 7 | 8 | var guess = function (userNumber) { 9 | if (userNumber === secret) { 10 | console.log("Well done!"); 11 | } else { 12 | console.log("Unlucky, try again."); 13 | } 14 | }; 15 | 16 | 17 | 18 | /* Further Adventures 19 | * 20 | * 1) Run the program and try a few guesses at the prompt. 21 | * e.g. guess(2) 22 | * 23 | * You can check if two values are NOT equal 24 | * by using the !== operator. 25 | * 26 | * 2) Change the condition in parentheses to check 27 | * if the guess is not equal to the secret. 28 | * 29 | * if (userNumber !== secret) 30 | * 31 | * 3) Run and test the updated program. 32 | * 33 | * 4) Change the messages to fit the new condition. 34 | * 35 | */ -------------------------------------------------------------------------------- /Ch12_Conditions/listing12.04.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 12.04 3 | * Guess the random number 4 | */ 5 | 6 | var getGuesser = function () { 7 | var secret = Math.floor(Math.random() * 10 + 1); 8 | 9 | return function (userNumber) { 10 | if (userNumber === secret) { 11 | return "Well done!"; 12 | } else { 13 | return "Unlucky, try again."; 14 | } 15 | }; 16 | }; 17 | 18 | var guess = getGuesser(); 19 | 20 | 21 | 22 | /* Further Adventures 23 | * 24 | * 1) Run the program. 25 | * 26 | * 2) Play the game a few times on the console. 27 | * e.g. guess(2) 28 | * 29 | * 3) Change the code so the secret number is 30 | * between 30 and 50. 31 | * 32 | * 4) Test your changes. 33 | * 34 | * CHALLENGE: Create a function called 'between' 35 | * that returns a random whole number between two 36 | * numbers passed as arguments. 37 | * 38 | * e.g. between(1, 5) // 1 <= whole number <= 5 39 | * between(100, 200) // 100 <= whole number <= 200 40 | * 41 | */ -------------------------------------------------------------------------------- /Ch12_Conditions/listing12.05.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 12.05 3 | * Higher or Lower 4 | */ 5 | 6 | var getGuesser = function () { 7 | var secret = Math.floor(Math.random() * 10 + 1); 8 | 9 | return function (userNumber) { 10 | if (userNumber === secret) { 11 | return "Well done!"; 12 | } else { 13 | if (userNumber > secret) { 14 | return "Too high!"; 15 | } else { 16 | return "Too low!"; 17 | } 18 | } 19 | }; 20 | }; 21 | 22 | var guess = getGuesser(); 23 | 24 | 25 | 26 | /* Further Adventures 27 | * 28 | * 1) Run the program 29 | * 30 | * 2) Play the game a few times. 31 | * e.g. guess(2) 32 | * 33 | * There are three blocks of code covering the 34 | * three outcomes for a guess: 35 | * too low, too high or correct. 36 | * 37 | * 3) Update the second condition to 38 | * (userNumber < secret) 39 | * 40 | * 4) Update the messages to match the 41 | * new condition. 42 | * 43 | * 5) Swap the first and second conditions so 44 | * the second condition uses the strict 45 | * equality operator. 46 | * 47 | * 6) Update the messages to match the 48 | * new condition. 49 | * 50 | */ -------------------------------------------------------------------------------- /Ch13_Modules/between.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * The 'between' function 3 | */ 4 | 5 | var between = function (lowest, highest) { 6 | var range = highest - lowest + 1; 7 | return lowest + Math.floor(Math.random() * range); 8 | }; 9 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.01.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.02.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 13.02 3 | * Loading JavaScript with a script tag 4 | * Requires the 'between' function 5 | */ 6 | 7 | var num = between(3, 7); 8 | console.log(num); 9 | 10 | 11 | /* Further Adventures 12 | * 13 | * 1) Run the program 4 or 5 times. 14 | * 15 | * A random number between 3 and 7 inclusive 16 | * should be generated each time. 17 | * 18 | * The 'between' function that generates 19 | * the number is loaded via a 20 | * script element in the HTML panel. 21 | * 22 | * 2) Create a rectangle object with random 23 | * length and width properties each 24 | * between 1 and 10 units long. 25 | * 26 | * 3) Define a showArea function that accepts 27 | * a rectangle as an argument and logs its 28 | * area to the console. 29 | * 30 | * > a 3 by 7 rectangle has area 21 31 | * 32 | */ -------------------------------------------------------------------------------- /Ch13_Modules/listing13.03.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.05.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.06.js: -------------------------------------------------------------------------------- 1 | // Get Programming with JavaScript 2 | // Listings 13.05 and 13.06 3 | // Using the number generator in the guessing game 4 | // tixina 5 | 6 | var getGuesser = function (lowest, highest) { 7 | var secret = between(lowest, highest); 8 | 9 | return function (userNumber) { 10 | if (userNumber === secret) { 11 | return "Well done!"; 12 | } else if (userNumber > secret) { 13 | return "Too high!"; 14 | } else { 15 | return "Too low!"; 16 | } 17 | }; 18 | }; 19 | 20 | var guess = getGuesser(5, 10); 21 | 22 | 23 | 24 | /* Further Adventures 25 | * 26 | * 1) Run the program and play the game. 27 | * 28 | * 2) Change the arguments to getGuesser so that 29 | * the number to guess is in a bigger range. 30 | * 31 | * 3) Update the game so that it keeps track of 32 | * the number of guesses a player takes to 33 | * find the secret number. 34 | * 35 | * > Well done! You took 6 guesses. 36 | * 37 | */ 38 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.07.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.08.js: -------------------------------------------------------------------------------- 1 | // Get Programming with JavaScript 2 | // Listings 13.07 and 13.08 3 | // Using spacer and between in the guessing game 4 | // Uses Number Generator and spacer 5 | 6 | var getGuesser = function (lowest, highest) { 7 | var secret = between(lowest, highest); 8 | 9 | return function (userNumber) { 10 | if (userNumber === secret) { 11 | return spacer.box("Well done!", 14, "="); 12 | } else if (userNumber > secret) { 13 | return spacer.box("Too high!", 13, "+"); 14 | } else { 15 | return spacer.box("Too low!", 12, "-"); 16 | } 17 | }; 18 | }; 19 | 20 | var guess = getGuesser(5, 10); 21 | 22 | 23 | 24 | /* Further Adventures 25 | * 26 | * 1) Report the number of guesses taken 27 | * at each stage. 28 | * 29 | * > guess(6) 30 | * 31 | * ------------------------ 32 | * - Too low! (2 guesses) - 33 | * ------------------------ 34 | * 35 | * 2) End the game after a certain number 36 | * of guesses. 37 | * 38 | * ****************** 39 | * ! Out Of Guesses ! 40 | * ****************** 41 | * 42 | */ 43 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.09.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.10.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listings 13.09 and 13.10 3 | * Using Kallie's code 4 | */ 5 | 6 | var getGuesser = function (lowest, highest) { 7 | var secret = between(lowest, highest); 8 | 9 | return function (userNumber) { 10 | var msg; 11 | if (userNumber === secret) { 12 | msg = dasher("Well done!"); 13 | return spacer.wrap(msg, msg.length + 4, "="); 14 | } else if (userNumber > secret) { 15 | msg = dasher("Too high!"); 16 | return spacer.wrap(msg, msg.length + 4, "+"); 17 | } else { 18 | msg = dasher("Too low!"); 19 | return spacer.wrap(msg, msg.length + 4, "-"); 20 | } 21 | }; 22 | }; 23 | 24 | 25 | var guess = getGuesser(5, 10); 26 | 27 | 28 | 29 | /* Further Adventures 30 | * 31 | * 1) Run the program. 32 | * 33 | * 2) Make a guess. 34 | * 35 | * !!! Where has spacer.wrap gone? !!! 36 | * 37 | */ 38 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.11.js: -------------------------------------------------------------------------------- 1 | // Get Programming with JavaScript 2 | // Listing 13.11 3 | // Kallie's formatting code 4 | 5 | var spreader = function (text, character) { 6 | return text.split("").join(character); 7 | }; 8 | 9 | var spacer = function (text) { 10 | return spreader(text, " "); 11 | }; 12 | 13 | var dasher = function (text) { 14 | return spreader(text, "-"); 15 | }; 16 | 17 | 18 | 19 | /* Further Adventures 20 | * 21 | * 1) Run the program. 22 | * 23 | * 2) Test out the three functions 24 | * at the prompt. 25 | * 26 | * > spacer("Too high!") 27 | * T o o h i g h ! 28 | * 29 | * > dasher("Too low!") 30 | * T-o-o- -l-o-w-! 31 | * 32 | * > spreader("Well done!", "*") 33 | * W*e*l*l* *d*o*n*e*! 34 | * 35 | */ 36 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.12.js: -------------------------------------------------------------------------------- 1 | // Get Programming with JavaScript 2 | // Listing 13.12 3 | // Kallie's formatting code in a namespace 4 | 5 | var kalliesCode = { 6 | spreader: function (text, character) { 7 | return text.split("").join(character); 8 | }, 9 | 10 | spacer: function (text) { 11 | return kalliesCode.spreader(text, " "); 12 | }, 13 | 14 | dasher: function (text) { 15 | return kalliesCode.spreader(text, "-"); 16 | } 17 | }; 18 | 19 | 20 | 21 | /* Further Adventures 22 | * 23 | * 1) Run the program. 24 | * 25 | * 2) Test out the three functions 26 | * at the prompt. 27 | * 28 | * > kalliesCode.spacer("Too high!") 29 | * T o o h i g h ! 30 | * 31 | * > kalliesCode.dasher("Too low!") 32 | * T-o-o- -l-o-w-! 33 | * 34 | * > kalliesCode.spreader("Well done!", "*") 35 | * W*e*l*l* *d*o*n*e*! 36 | * 37 | * 3) Create a function called squasher 38 | * that removes characters added by spreader. 39 | * 40 | * > kalliesCode.squasher("T-o-o- -h-i-g-h-!", "-") 41 | * Too high! 42 | * 43 | */ 44 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.13.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.17.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Ch13_Modules/listing13.18.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listings 13.17 and 13.18 3 | * Importing modules for The Crypt 4 | */ 5 | 6 | var game = theCrypt.getGame(); 7 | 8 | 9 | 10 | /* Further Adventures 11 | * 12 | * 1) Run the program and play the game. 13 | * 14 | * > game.get() 15 | * > game.go("east") 16 | * 17 | */ 18 | -------------------------------------------------------------------------------- /Ch14_Models/TheSparrow.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Ch14_Models/TheSparrow.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Chapter 14 3 | * The Sparrow 4 | */ 5 | 6 | var game = theCrypt.getGame(); 7 | 8 | 9 | 10 | /* Further Adventures 11 | * 12 | * 1) Play the game, Using the public methods: 13 | * 14 | * > game.get() 15 | * > game.go("up") 16 | * 17 | */ 18 | -------------------------------------------------------------------------------- /Ch14_Models/listing14.01.js: -------------------------------------------------------------------------------- 1 | // Get Programming with JavaScript 2 | // Listing 14.01 3 | // The User constructor 4 | 5 | var User = function (name) { 6 | var sessions = []; 7 | var totalDuration = 0; 8 | 9 | this.addSession = function (sessionDate, duration) { 10 | sessions.push({ 11 | "sessionDate" : sessionDate, 12 | "duration" : duration 13 | }); 14 | totalDuration += duration; 15 | return totalDuration; 16 | }; 17 | 18 | this.getData = function () { 19 | return { 20 | "name" : name, 21 | "total" : totalDuration, 22 | "sessions": sessions.slice() 23 | }; 24 | }; 25 | }; 26 | 27 | 28 | 29 | /* Further Adventures 30 | * 31 | * 1) Run the program. 32 | * 33 | * 2) Test the constructor at the console prompt: 34 | * 35 | * > var user = new User("Mahesha") 36 | * > user.addSession("2017-02-05", 120) 37 | * > user.addSession("2017-02-06", 35) 38 | * > user.getData() 39 | * > user.getData().total 40 | * 41 | */ 42 | -------------------------------------------------------------------------------- /Ch14_Models/listing14.02.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Ch14_Models/listing14.03.js: -------------------------------------------------------------------------------- 1 | // Get Programming with JavaScript 2 | // Listings 14.02 and 14.03 3 | // A function to build a user model from user data 4 | // Requires the User constructor 5 | 6 | var buildUser = function (userData) { 7 | var user = new User(userData.name); 8 | 9 | userData.sessions.forEach(function (sesh) { 10 | user.addSession(sesh.sessionDate, sesh.duration); 11 | }); 12 | 13 | return user; 14 | }; 15 | 16 | var userData = { 17 | "name" : "Mahesha", 18 | "sessions" : [ 19 | { "sessionDate" : "2017-02-05", "duration" : 120 }, 20 | { "sessionDate" : "2017-02-06", "duration" : 35 }, 21 | { "sessionDate" : "2017-02-06", "duration" : 45 } 22 | ] 23 | }; 24 | 25 | var user = buildUser(userData); 26 | 27 | console.log(user.addSession("2017-02-15", 40)); 28 | 29 | 30 | 31 | /* Further Adventures 32 | * 33 | * 1) Run the program. 34 | * 35 | * 2) Add a couple more sessions to the user data 36 | * and run the program again. 37 | * 38 | * 3) Use buildUser to create a second user from 39 | * a new user data object. 40 | * 41 | */ 42 | -------------------------------------------------------------------------------- /Ch14_Models/listing14.08.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Ch15_Views/Listing15.02.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Ch15_Views/Listing15.03.js: -------------------------------------------------------------------------------- 1 | // Get Programming with JavaScript 2 | // Listings 15.02 and 15.03 3 | // Testing two user views 4 | 5 | var user = new fitnessApp.User("Mahesha"); 6 | 7 | user.addSession("2017-02-05", 35); 8 | user.addSession("2017-02-06", 45); 9 | 10 | fitnessApp.userView.render(user); 11 | fitnessApp.userViewEnhanced.render(user); 12 | 13 | 14 | 15 | /* Further Adventures 16 | * 17 | * 1) Add a couple more sessions 18 | * for the user and run the 19 | * program again. 20 | * 21 | * 2) Take a look at the code for 22 | * the enhanced view (it's imported 23 | * on the HTML panel). 24 | * Can you follow it? 25 | * 26 | */ 27 | -------------------------------------------------------------------------------- /Ch15_Views/Listing15.08.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 15.08 3 | * A message view 4 | */ 5 | 6 | (function () { 7 | 8 | var getMessageInfo = function (messageData) { 9 | return "*** " + messageData + " ***"; 10 | }; 11 | 12 | var render = function (message) { 13 | console.error(getMessageInfo(message)); 14 | }; 15 | 16 | if (window.theCrypt === undefined) { 17 | window.theCrypt = {}; 18 | } 19 | 20 | theCrypt.messageView = { 21 | render: render 22 | }; 23 | 24 | })(); 25 | 26 | 27 | 28 | /* Further Adventures 29 | * 30 | * 1) Run the program. 31 | * 32 | * 2) Create a shortcut variable 33 | * for the message view. 34 | * 35 | * > var mv = theCrypt.messageView 36 | * 37 | * 3) Display some messages. 38 | * 39 | * > mv.render("Hello") 40 | * > mv.render("wassup wassup wassup") 41 | * 42 | * 4) Change the program to display 43 | * messages like this: 44 | * 45 | * > mv.render("Hello") 46 | * The message is: Hello 47 | * 48 | * 5) Change the program to display 49 | * messages in upper case. 50 | * 51 | * > mv.render("Hello") 52 | * HELLO 53 | * 54 | */ 55 | -------------------------------------------------------------------------------- /Ch16_Controllers/Listing16.12.js: -------------------------------------------------------------------------------- 1 | /* Get Programming with JavaScript 2 | * Listing 16.12 3 | * Running the game 4 | */ 5 | 6 | var playerName = "Jahver"; 7 | var map = theCrypt.mapData; 8 | 9 | game.init(map, playerName); 10 | 11 | 12 | 13 | /* Further Adventures 14 | * 15 | * 1) Run the program. 16 | * 17 | * 2) Play the game. 18 | * 19 | */ 20 | -------------------------------------------------------------------------------- /Ch16_Controllers/listing16.02.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Ch16_Controllers/listing16.03.js: -------------------------------------------------------------------------------- 1 | // Get Programming with JavaScript 2 | // Listings 16.02 and 16.03 3 | // The fitness app 4 | 5 | var app = fitnessApp.init(fitnessApp.userData); 6 | 7 | 8 | 9 | /* Further Adventures 10 | * 11 | * 1) Create your own user data file 12 | * on JS Bin and import it instead 13 | * of the current data file. 14 | * 15 | * 2) Log some sessions for your user. 16 | * 17 | */ 18 | -------------------------------------------------------------------------------- /Ch16_Controllers/listing16.11.html: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Ch17_HTML/listing17.01.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |Brief info about my favorite movies.
12 | 13 |An emotional adventure inside the head of a young girl.
18 | 19 | 20 | 21 | 22 | 23 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Ch17_HTML/listing17.03.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |Welcome!
11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Ch17_HTML/listing17.04.js: -------------------------------------------------------------------------------- 1 | // Get Programming with JavaScript 2 | // Listings 17.03 and 17.04 3 | // Adding content to a paragraph with JavaScript 4 | 5 | (function () { 6 | 7 | function getGreeting () { 8 | var hellos = [ 9 | "Nanu nanu!", 10 | "Wassup!", 11 | "Yo!", 12 | "Hello movie lover!", 13 | "Ay up me duck!", 14 | "Hola!" 15 | ]; 16 | 17 | var index = Math.floor(Math.random() * hellos.length); 18 | 19 | return hellos[index]; 20 | } 21 | 22 | function updateGreeting () { 23 | para.innerHTML = getGreeting(); 24 | } 25 | 26 | var para = document.getElementById("greeting"); 27 | 28 | updateGreeting(); 29 | 30 | })(); 31 | 32 | 33 | 34 | /* Further Adventures 35 | * 36 | * 1) Add a heading to the HTML, 37 | * before the paragraph. 38 | * 39 | * 2) Give the heading an id attribute. 40 | * 41 | * 3) Use JavaScript to update the heading. 42 | * 43 | */ 44 | -------------------------------------------------------------------------------- /Ch17_HTML/listing17.05.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |" + movie.summary + "
"; 24 | return html; 25 | } 26 | 27 | function getMoviesHTML (movies) { 28 | var html = ""; 29 | 30 | movies.forEach(function (movie) { 31 | html += "Welcome!
13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Ch18_Controls/listing18.03.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |Brief info about my favorite movies.
13 | 14 |An emotional adventure inside the head of a young girl.
19 |I'm going on an adventure!
-------------------------------------------------------------------------------- /Ch23_Express/webserver/public/client-server-GET.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jrlarsen/GetProgramming/9f89c8aaaaecd518c6fea61de94a4ecd129ab03a/Ch23_Express/webserver/public/client-server-GET.png -------------------------------------------------------------------------------- /Ch23_Express/webserver/public/client-server.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |The steps involved in requesting a resource.
12 |
13 |
17 |
The data is sent to the server automatically.
11 | 12 | 13 | -------------------------------------------------------------------------------- /Ch23_Express/webserver/public/index.html: -------------------------------------------------------------------------------- 1 |We choose to go to the moon
-------------------------------------------------------------------------------- /Ch23_Express/webserver/static.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var app = express(); 3 | 4 | app.use(express.static('public')); 5 | 6 | app.listen(1337); -------------------------------------------------------------------------------- /Ch23_Express/webserver/threeRoutes.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var app = express(); 3 | 4 | app.get('/', sayHello); 5 | app.get('/moon', goMoon); 6 | app.get('/adventure', setOff); 7 | 8 | function sayHello (req, res) { 9 | res.send('We choose to go to the moon.
'; 15 | res.send(html); 16 | } 17 | 18 | function setOff (req, res) { 19 | res.send('I’m going on an adventure!
'); 20 | } 21 | 22 | app.listen(1337); -------------------------------------------------------------------------------- /Ch24_Polling/HighFives_XHR/auctionApp.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var app = express(); 3 | 4 | var bid = 5; 5 | 6 | app.get('/state', getState); 7 | app.post('/bid', makeBid); 8 | 9 | function makeBid (req, res) { 10 | bid = bid + 5; 11 | res.json({ bid: bid }); 12 | } 13 | 14 | function getState (req, res) { 15 | res.json({ bid: bid }); 16 | } 17 | 18 | app.use(express.static("public")); 19 | 20 | app.listen(1337); -------------------------------------------------------------------------------- /Ch24_Polling/HighFives_XHR/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "high-fives", 3 | "version": "1.0.0", 4 | "dependencies": { 5 | "express": "^4.13.4" 6 | } 7 | } -------------------------------------------------------------------------------- /Ch24_Polling/HighFives_XHR/public/auction.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin:0; 3 | padding: 0; 4 | text-align:center; 5 | font-family:"Helvetica Neue", Helvetica, sans-serif; 6 | } 7 | 8 | h1 { 9 | background-color:rebeccapurple; 10 | color: #ffc; 11 | padding:0.5em; 12 | margin:0 0 1em; 13 | } 14 | 15 | table { 16 | width: 80%; 17 | margin:0 auto; 18 | border-collapse: collapse; 19 | } 20 | 21 | th, td { 22 | padding:0.3em; 23 | } 24 | 25 | th { 26 | background-color:rebeccapurple; 27 | color: #ffc; 28 | } 29 | 30 | td { 31 | border-bottom:1px solid rebeccapurple; 32 | } 33 | 34 | #bid { 35 | background-color: #eef; 36 | width:20%; 37 | } 38 | 39 | #asking { 40 | background-color: #fee; 41 | width:20%; 42 | } -------------------------------------------------------------------------------- /Ch24_Polling/HighFives_XHR/public/auction.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |Item | 15 |Bid | 16 |Asking | 17 |Action | 18 |
---|---|---|---|
The Fruitinator! Action Figure | 23 |...loading... | 24 |...loading... | 25 |26 | |
Item | 15 |Bid | 16 |Asking | 17 |Action | 18 |
---|---|---|---|
The Fruitinator! Action Figure | 23 |...loading... | 24 |...loading... | 25 |26 | |
12 | 13 | 14 | 15 |
16 | 17 |Item | 21 |Bid | 22 |Asking | 23 |Action | 24 |
---|---|---|---|
29 | | 30 | | 31 | | 32 | |