|
| 1 | +goals do |
| 2 | + goal "Setup your programming environment" |
| 3 | + goal "Be able to use the basic building blocks of JavaScript code" |
| 4 | + goal "Do simple calculations" |
| 5 | + goal "Use and understand variables" |
| 6 | + goal "Use and understand arrays" |
| 7 | + goal "Use loops and conditional statements" |
| 8 | +end |
| 9 | + |
| 10 | +overview do |
| 11 | + message <<-MARKDOWN |
| 12 | + JavaScript is a fully featured programming language with variables, loops, |
| 13 | + and conditionals. Just like Ruby, Java, Python, and PHP, you can use JavaScript |
| 14 | + to do math, model large systems, and perform complex calculations – all in your browser! |
| 15 | + In this lesson, we'll learn about the fundamentals of the JavaScript programming language. |
| 16 | + MARKDOWN |
| 17 | +end |
| 18 | + |
| 19 | +steps do |
| 20 | + step "Launch your programming environment" do |
| 21 | + message <<-MARKDOWN |
| 22 | + |
| 23 | + When programming, you'll generally want these tools on hand: |
| 24 | + |
| 25 | + * Your browser to see the code running (I recommend Chrome) |
| 26 | + * A text editor to change the code (I recommend <a |
| 27 | + href="http://www.sublimetext.com/">Sublime</a>) |
| 28 | + * A javascript console so you can experiment and print out debugging |
| 29 | + messages. This is built into your browser. |
| 30 | + |
| 31 | + Some helpful links for browser consoles: |
| 32 | + |
| 33 | + * <a href="https://developer.chrome.com/devtools">How to launch dev tools in chrome.</a> |
| 34 | + * <a href="https://msdn.microsoft.com/en-us/library/ie/bg182326.aspx">How to launch dev tools in internet explorer.</a> |
| 35 | + * <a href="https://developer.mozilla.org/en-US/docs/Tools/Browser_Console">How to launch dev tools in firefox.</a> |
| 36 | + * <a href="https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/GettingStarted/GettingStarted.html">How to launch dev tools in safari.</a> |
| 37 | + |
| 38 | + Once you have these tools available, we need to open the files we'll be working with: |
| 39 | + |
| 40 | + * Open game/snake.js in your text editor |
| 41 | + * Open game/index.html in your browser |
| 42 | + |
| 43 | + |
| 44 | + Now open up your browser's developer tools, and click over to the console tab. Type the following code: |
| 45 | + MARKDOWN |
| 46 | + |
| 47 | + source_code :javascript, <<-JAVASCRIPT |
| 48 | + console.log('programming!'); |
| 49 | + JAVASCRIPT |
| 50 | + |
| 51 | + message <<-MARKDOWN |
| 52 | + Press enter. Your browser should now look like this: |
| 53 | + <img src="img/browser_console.png" alt="image of browser console with console.log"></img> |
| 54 | + |
| 55 | + Congrats, you just wrote your first lines of JavaScript code! __console.log__ is an important function – |
| 56 | + it allows you to print information to the browser's console. It's very helpful in debugging! You can also |
| 57 | + use the __alert__ function to make a message pop up in the browser. Try it out! |
| 58 | + MARKDOWN |
| 59 | + end |
| 60 | + |
| 61 | + step do |
| 62 | + message "Next try some simple math that's built into JavaScript. Type these lines into console:" |
| 63 | + |
| 64 | + source_code :javascript, <<-JAVASCRIPT |
| 65 | + 3 + 3 |
| 66 | + 7 * 6 |
| 67 | + JAVASCRIPT |
| 68 | + end |
| 69 | + |
| 70 | + step do |
| 71 | + message "**Variables** are names with values assigned to them." |
| 72 | + |
| 73 | + source_code :javascript, <<-JAVASCRIPT |
| 74 | + var myVariable = 5 |
| 75 | + JAVASCRIPT |
| 76 | + |
| 77 | + message "This assigns the value `5` to the name `myVariable`." |
| 78 | + end |
| 79 | + |
| 80 | + step do |
| 81 | + message "You can also do math with variables:" |
| 82 | + source_code :javascript, <<-JAVASCRIPT |
| 83 | + myVariable + 2 |
| 84 | + myVariable * 3 |
| 85 | + JAVASCRIPT |
| 86 | + end |
| 87 | + |
| 88 | + step do |
| 89 | + message "Variables can also hold more than just numbers and text. Another type of data in Javascript is called an **array**." |
| 90 | + |
| 91 | + source_code :javascript, 'var fruits = ["kiwi", "strawberry", "plum"]' |
| 92 | + message <<-MARKDOWN |
| 93 | + Here we're using the variable `fruits` to hold a collection of fruit names. |
| 94 | + An array, designated by the `[ ]` (square-brackets), is a list of data that can be referenced by its index. |
| 95 | + The index is the position inside the array, each separated by a comma. Arrays use 0-based indices, so the first element in an array is at Index 0, the second is at index 1, and so on. |
| 96 | + For example, in the array above, to get the string 'strawberry' in Javascript, you would type 'fruits[1]', which Javascript understands as: get the value at index 1 in the array fruits. |
| 97 | + MARKDOWN |
| 98 | + end |
| 99 | + |
| 100 | + step do |
| 101 | + message "Arrays are a type of __object__ in JavaScript that are designated by the use of square brackets. Objects of all types in javascript often include helpful attributes!" |
| 102 | + |
| 103 | + source_code :javascript, <<-JAVASCRIPT |
| 104 | + fruits.length |
| 105 | + JAVASCRIPT |
| 106 | + |
| 107 | + message <<-MARKDOWN |
| 108 | +Objects can also have __functions__, which can be helpful for altering objects and learning more about them. |
| 109 | +Functions are __invoked__ with parentheses, which causes them to run. |
| 110 | + MARKDOWN |
| 111 | + |
| 112 | + source_code :javascript, <<-JAVASCRIPT |
| 113 | + fruits.push("orange") |
| 114 | + fruits.slice(1) |
| 115 | + JAVASCRIPT |
| 116 | + |
| 117 | + message <<-MARKDOWN |
| 118 | +The __push__ function allows us to add new items to an array. the slice function returns a new array with |
| 119 | +with everything to the right of the __index__ we provided. Here, we passed the function the number 1, so |
| 120 | +slice returned an array with everything after the first element in the array. (Note that the first element is assigned 0 as its index rather than 1).) |
| 121 | + MARKDOWN |
| 122 | + end |
| 123 | + |
| 124 | + step do |
| 125 | + message "You can also make your own functions:" |
| 126 | + |
| 127 | + source_code :javascript, <<-JAVASCRIPT |
| 128 | + var pluralize = function(word) { |
| 129 | + return word + "s" |
| 130 | + } |
| 131 | + pluralize("kiwi") |
| 132 | + JAVASCRIPT |
| 133 | + |
| 134 | + message "Functions take **parameters**, which are the variables they work on. In this case, we made a function called pluralize that takes one parameter, a word." |
| 135 | + |
| 136 | + message "Functions can also return data. In this case, pluralize returns the word with an 's' added to the end of it. In order for a function to return something, you have to use the __return__ keyword." |
| 137 | + end |
| 138 | + |
| 139 | + |
| 140 | + step do |
| 141 | + message "Arrays have a function called **forEach** which iterates through the list running code on each item. It takes another function as a parameter." |
| 142 | + source_code :javascript, <<-JAVASCRIPT |
| 143 | + fruits.forEach(function(fruit) { |
| 144 | + console.log(fruit) |
| 145 | + }) |
| 146 | + JAVASCRIPT |
| 147 | + message "This takes the first item from the `fruits` array (`\"strawberry\"`), assigns it to the variable `fruit`, and runs the code between curly brackets. Then it does the same thing for each other item in the list. The code above should print a list of the fruits." |
| 148 | + end |
| 149 | + |
| 150 | + step do |
| 151 | + message "A **conditional** runs code only when a statement evaluates to true." |
| 152 | + |
| 153 | + source_code :javascript, <<-JAVASCRIPT |
| 154 | + if(myVariable > 1) { |
| 155 | + console.log('YAY') |
| 156 | + } |
| 157 | + JAVASCRIPT |
| 158 | + |
| 159 | + message "This prints `YAY!` if the value stored in `myVariable` is greater than 1." |
| 160 | + |
| 161 | + message "Try changing the `>` in the conditional to a `<`." |
| 162 | + end |
| 163 | + |
| 164 | +end |
| 165 | + |
| 166 | +next_step "lesson-2" |
0 commit comments