)`
```js
let age = 14;
if (age >= 13 && age <= 19) {
console.log("You're a teen!");
}
if (age < 13 || age > 19) {
console.log('You are not a teen!');
}
```
---
class: middle
Reworking last weeks guessing example:
```js
/* __//
/.__.\
\ \/ /
'__/ \
\- )
\_____/ -cf
_____|_|____ */
let eggs = '000000000000000000000000000000000000000000000000';
let guess = window.prompt("Guess how many eggs?", 5);
console.log("You guessed: " + guess);
// how many eggs do we have?
if (guess < eggs.length || guess > eggs.length) {
console.log("you're wrong!");
} else if (guess == eggs.length) {
console.log("bingo!");
}
// or could be written:
if (guess == eggs.length) {
console.log("bingo!");
} else {
console.log("you're wrong!");
}
```
---
class: middle
Let's recap how we access html elements in our Javascript code with `querySelector`.
Remember that `querySelector` is a way to search for elements in our document based on *selectors* — like the
css selectors we've learned about:
```js
let blogPost = document.querySelector('#blog');
```
Document refers to the `DOM` document.
You can even use `pseudo-classes`:
```js
let lastGraph = document.querySelector('p:last-child');
```
---
class: middle
example.js:
```js
let blogElement = document.querySelector('#blog');
console.log(blogElement);
```
example.html:
```html
hello class
```
hello class
---
class: middle
Now that we have a reference to our element in javascript, we can do something with it
like adding a class:
```css
.red {
color: red;
}
```
The `classList` attribute of a html element in javascript provides tools for adding, removing, and checking
whether an element has a class.
```js
let blogElement = document.querySelector('#blog');
blogElement.classList.add('red');
```
hello class
---
class: middle center
### Now let's learn another programming language concept
---
class: middle
#### Functions
Functions are a powerful concept that most programming languages employ.
They allow us to set aside a group of instructions or code to execute at some later point.
Functions are composed by specifying the `function` keyword and then providing a custom name for your `function`.
The name is how we will later refer to it.
```js
function myFunction() {
// code to run later when we call the function goes in here
}
```
at some later point...
```js
myFunction(); // lets call our function
```
---
class: middle
Functions can optionally receive additional details, called `arguments`, to make
them more extensible and useful:
```js
function sayHi() {
console.log('Hi Josephine.');
}
sayHi();
sayHi(); // say hi again
```
Instead of making the message we write to console fixed, we could use an `argument` to
make the message we write dynamic:
```js
function sayHello(name) {
console.log('oh, hello ' + name);
}
sayHello('Josephine');
sayHello('Maryam'); // say hello to someone new
```
`arguments` within a `function` are `variables` that only exist within the `scope` or context
of the function.
---
class: middle
An important aspect of functions is that they help break up our code so that we don't
have to repeat ourselves by copying and pasting code.
We could instead incapsulate that code in a function and call it whenever we want
to preform that set of actions.
```js
// from our previous example
let blogElement = document.querySelector('#blog');
blogElement.classList.add('red');
let blogElement = document.querySelector('footer');
footer.classList.add('red');
// lets make this more reusable
function makeRed(element) {
element.classList.add('red');
}
let blogElement = document.querySelector('#blog');
makeRed(blogElement);
makeRed(document.querySelector('footer'));
```
My Blog
---
class: middle
Another useful thing functions can do is send back a *value* from the code it contains.
We use the `return` keyword to send back some value to the place where it was called.
```js
function scream(text){
return text.toUpperCase() + '!!!!';
}
let shouting = scream('hello class');
console.log(shouting);
console.log(scream('I hope this is all making sense 😅'));
```
So we can define a list of instructions that we might need to repeat often, call it at
whatever point we want, but also return some result from that work to use.
---
class: middle
### Adding interactivity with Javascript
Now that know how to reference an html element in our page, we can listen
for events on that element and perform actions based on it.
To listen for events on an element, we use `addEventListener`.
---
class: middle
#### addEventListener()
The `addEventListener` method tells the browser to listen or watch an element for events
that we specify and then call a function to run some code once that event occurs.
We add listeners to the reference for a html element that we retreived from `document.querySelector`:
```html
```
```js
function clickMe() {
console.log('thanks for click~');
}
let button = document.querySelector('.click-me');
button.addEventListener('click', clickMe);
// ^ event ^ function to call when event occurs
```
---
class: middle
Note that when we provide the function we want to be called to the listener, we don't include the `()` parentheses.
```js
button.addEventListener('click', clickMe);
```
When we use `clickMe()` with the parentheses on functions, the function is called at that moment.
```js
// wrong ! : ((
button.addEventListener('click', clickMe());
```
But since we want our function to be called later on when the event occurs, we only need to pass the
`reference` or name of the function, and not the `clickMe()` full invocation of it.
---
class: middle
There are all kinds of events we can respond to, for instance `mouseover`:
```js
function hoverElement(event) {
console.log(event);
}
let poem = document.querySelector('.borges-poem');
poem.addEventListener('mouseover', hoverElement);
```
```html
I, who have felt the horror of mirrors
Not only in front of the impenetrable crystal
Where there ends and begins, uninhabitable,
An impossible space of reflections,
But of gazing even on water that mimics
The other blue in its depth of sky,
That at times gleams back the illusory flight
Of the inverted bird, or that ripples,
```
.footnote[poem via http://www.cabinetmagazine.org/issues/14/borges.php]
---
class: middle
I, who have felt the horror of mirrors
Not only in front of the impenetrable crystal
Where there ends and begins, uninhabitable,
An impossible space of reflections,
But of gazing even on water that mimics
The other blue in its depth of sky,
That at times gleams back the illusory flight
Of the inverted bird, or that ripples,
---
class: middle
As you can see, our event has a bunch of info, but one thing of interest is the `target` or the thing triggering the event.
Let's log the target to the console:
```js
function hoverElement(event) {
console.log(event.target.tagName);
}
let poem = document.querySelector('.borges-poem');
poem.addEventListener('mouseover', hoverElement);
```
I, who have felt the horror of mirrors
Not only in front of the impenetrable crystal
Where there ends and begins, uninhabitable,
An impossible space of reflections,
But of gazing even on water that mimics
The other blue in its depth of sky,
That at times gleams back the illusory flight
Of the inverted bird, or that ripples,
---
class: middle
We could add a `conditional` that checks whether we're hovering over a span element:
```js
function hoverElement(event) {
if (event.target.tagName == 'SPAN') {
event.target.classList.add('yellow');
}
}
let poem = document.querySelector('.borges-poem');
poem.addEventListener('mouseover', hoverElement);
```
Let's also add some transitioning css:
```css
span {
transition: background .5s ease-in-out;
}
```
---
class: middle
I, who have felt the horror of mirrors
Not only in front of the impenetrable crystal
Where there ends and begins, uninhabitable,
An impossible space of reflections,
But of gazing even on water that mimics
The other blue in its depth of sky,
That at times gleams back the illusory flight
Of the inverted bird, or that ripples,