diff --git a/step1-01/css-demo/README.md b/step1-01/css-demo/README.md index 24f59e0..09bf1d0 100644 --- a/step1-01/css-demo/README.md +++ b/step1-01/css-demo/README.md @@ -124,12 +124,6 @@ a:hover { color: #ccc; } -/* Sibling selector */ -a ~ a { - /* Changing elements from inline to block */ - display: block; -} - /* Positional pseudo-selector */ .tiles > div:last-child { /* overrides margin-right but leaves other margins alone */ diff --git a/step1-01/css-demo/css-demo.css b/step1-01/css-demo/css-demo.css index e69de29..35bb9de 100644 --- a/step1-01/css-demo/css-demo.css +++ b/step1-01/css-demo/css-demo.css @@ -0,0 +1,62 @@ +/* body { + font: 1.2em sans-serif; +} */ + +/* h1 { + color: black; + background: #ababab; + margin-bottom: 15px; + margin-top: 15px; + padding: 10px; + border: 1px solid #ddd; +} */ + +/* span { + color: #004578; +} */ + +/* a ~ a { + display: block; +} */ + +/* .tiles { + display: flex; +} */ + +/* .tiles img { + width: 100%; +} */ + +/* .tiles > div { + background: rgb(10, 10, 10); + color: white; + flex-basis: 100%; + padding: 10px 20px 15px; + margin: 10px 20px 10px 0; + border: 1px solid white; +} */ + +/* div.important-links { + background: #004578; +} */ + +/* a { + color: white; +} */ + +/* a:hover { + color: #ccc; +} */ + +/* .tiles > div:last-child { + margin-right: 0; +} */ + +/* #contact-form { + display: flex; + flex-direction: column; +} */ + +/* input[type='submit'] { + margin-top: 10px; +} */ diff --git a/step1-01/js-demo/README.md b/step1-01/js-demo/README.md index 7c494b7..3343979 100644 --- a/step1-01/js-demo/README.md +++ b/step1-01/js-demo/README.md @@ -19,7 +19,7 @@ By the end of the demo we'll have covered the following: - Loops - Interacting with the DOM (Document Object Model) -### Variables +### Introduction To Variables We can create a new variable with the keywords `var`, `let`, `const` and use them within our application. These variables can contain one of the following types of values: @@ -34,6 +34,8 @@ We can create a new variable with the keywords `var`, `let`, `const` and use the > JavaScript is a loosely typed (dynamic) language, so if you initially store a number in a variable (`let myVar = 0`), you can change it to contain a string by simply writing `myVar = 'hello'` without any trouble. +### Adding Variables + Let's start off our demo by adding some variables to our [script tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script). The other examples on this page will reference these variables. ```js @@ -41,11 +43,13 @@ const match = 'a'; let matches = 0; ``` +> Note that `const` variables are those that will never change during the duration of your program, whereas `let` can change over time. Our `match` value will stay constant, but our `matches` will increment as we find matches. + ### Functions -Functions are reusable pieces of functionality. Functions can take inputs (parameters) and return values. Functions can be called from within your program, from within other functions, or invoked from within the DOM itself. +Functions are reusable pieces of functionality. Functions can take inputs (parameters) and return values (or neither). Functions can be called from within your program, from within other functions, or invoked from within the DOM itself. -In our example we'll create a function called `displayMatches` (camelCase is typical for functions) and we'll invoke this function any time that our submit button is clicked. For now we'll simply have our function call `console.log` (a function that prints values to the browser console): +In our example we'll create a function called `displayMatches` (camelCase is typical for functions) and we'll invoke this function every time that our submit button is clicked. For now we'll simply have our function call `console.log` (a function that prints values to the browser console): ```html @@ -59,7 +63,7 @@ function displayMatches() { ### Iteration -Next we'll update our function to iterate through a string of text. Later, we'll find and count all the `a` characters in the `email` variable, but for now, we'll just separately log each letter of a placeholder `text` variable. We loop over each letter using the [`for of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) syntax. +Next we'll update our function to iterate through a string of letters. We loop over each letter using the [`for of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) syntax. We'll use real input later, but for now this varifies that our function is working. ```js function displayMatches() { @@ -72,7 +76,7 @@ function displayMatches() { ### Conditionals -Next we want to compare each `letter` with our `match` value, and if they are the same, we will increment our `matches` variable. Notice that `match` is a `const` and `matches` is a `let` because we expected `matches` to change over time. Remember that `letter = match` would set the `letter` variable to the value in `match`--so to do comparisons, we use the equality operator `==` or the strict equality operator `===`. +Next we want to compare each `letter` with our `match` value, and if they are the same, we will increment our `matches` variable. Remember that `letter = match` would set the `letter` variable to the value in `match`, so to do comparisons, we use the equality operator `==` or the strict equality operator `===`. ```js function displayMatches() { @@ -95,14 +99,22 @@ Now that we have a function performing all of our logic, it's time to connect th First we need to get a reference to the email field in our app's DOM. To do this, I've added an `id` to the input, and we will call one of JavaScript's oldest methods (IE 5.5), `getElementById`, which we find on the browser-provided `document` global variable. This function will return a reference to that input, and we can store it in the `email` variable. ```js -const email = document.getElementById('email'); -console.log(email); +function displayMatches() { + const email = document.getElementById('email'); + console.log(email); + ... +} ``` Since what we're actually after is the value of the input field, we can set our `text` variable to the string assigned to the email input's `value` key. To see this in action, in Chrome you can right click on the console message created by the code above, choose "save as variable" and then type `variableName.value`. ```js -const text = email.value; +function displayMatches() { + const email = document.getElementById('email'); + const text = email.value; + console.log(text); + ... +} ``` #### Returning the values to the DOM @@ -110,19 +122,29 @@ const text = email.value; Now that we've read values from the DOM and fed that into our matching logic, we are ready to return the number of matches to our app. To do this we first need to grab a reference to our submit button, and since this button has no `id` we are going to use the more modern (IE8+) `querySelector` to get it. This function takes any valid CSS selector and returns the first match found. ```js -const submit = document.querySelector('.submit'); +function displayMatches() { + ... + const submit = document.querySelector('.submit'); +} ``` Now that we have a reference to the submit input, we can set its value contain to the number of matches. ```js -submit.value = matches + ' matches'; +function displayMatches() { + ... + const submit = document.querySelector('.submit'); + submit.value = matches + ' matches'; +} ``` We could also have done this in a single line as follows: ```js -document.querySelector('.submit').value = matches + ' matches'; +function displayMatches() { + ... + document.querySelector('.submit').value = matches + ' matches'; +} ``` ## Next Step diff --git a/step1-01/js-demo/js-demo.html b/step1-01/js-demo/js-demo.html index 7aa4459..e46faab 100644 --- a/step1-01/js-demo/js-demo.html +++ b/step1-01/js-demo/js-demo.html @@ -4,23 +4,12 @@