ch01 — Functions

We start from equations, not syntax

What is F12 Console?

Try it now:

1 + 1

You should see 2.

Equation = Function

Everyone already knows this:

f(x) = x + 1

This is a function. That’s it.

Your First Code

f(x) = x + 1 in JavaScript:

const f = x => x + 1

f(1)
f(10)
f(100)

Paste it in F12 Console. You just ran your first function.

Another Function

double(x) = x * 2

const double = x => x * 2

double(3)
double(10)
double(0)

The pattern is always the same:

name(input) = ruleconst name = input => rule

Try More

const square = x => x * x
square(3)
square(5)
const half = x => x / 2
half(10)
half(100)

Make your own! Change the rule, see what happens.

Same Input = Same Output

const double = x => x * 2
double(5)
double(5)
double(5)

Always 10. Every time. No surprises.

Combine Functions

Build bigger rules from smaller rules:

const add1 = x => x + 1
const double = x => x * 2

const add1ThenDouble = x => double(add1(x))

add1ThenDouble(3)
// add1(3) = 4, double(4) = 8

ch01 Summary

ch02 — Logic

In ch01 we learned: input → rule → output

Now we learn how to make decisions

If It Rains, the Ground Gets Wet

We already use logic in daily life:

This is if P → Q

If in JavaScript

const weather = "rain"

if (weather === "rain") {
  console.log("Bring an umbrella")
}

If…Else

What if it’s NOT raining?

const weather = "sunny"

if (weather === "rain") {
  console.log("Bring an umbrella")
} else {
  console.log("Enjoy the sunshine")
}

True and False

Conditions are either true or false:

3 > 1    // true
5 < 2    // false
3 === 3  // true
3 === 5  // false
Symbol Meaning
=== is equal to
!== is not equal to
> greater than
< less than
>= greater or equal
<= less or equal

If…Else If…Else

Multiple conditions:

const temp = 35

if (temp > 30) {
  console.log("Very hot")
} else if (temp > 20) {
  console.log("Nice weather")
} else {
  console.log("Bring a jacket")
}

Checks from top to bottom. Runs the first one that’s true.

Logic Inside Functions

Put logic inside a function — still input → output:

const advice = temp => {
  if (temp > 30) {
    return "Drink water"
  } else if (temp > 20) {
    return "Nice weather"
  } else {
    return "Bring a jacket"
  }
}

advice(35)
advice(25)
advice(10)

And, Or, Not

Combine conditions:

// AND: both must be true
if (age >= 18 && hasTicket) {
  console.log("You can enter")
}

// OR: at least one true
if (age < 12 || age >= 65) {
  console.log("Discount")
}

// NOT: flip
if (!hasTicket) {
  console.log("Buy a ticket")
}
Symbol Meaning
&& AND
\|\| OR
! NOT

Real Example: Restaurant

const getPrice = (item, isMember) => {
  if (item === "coffee") {
    if (isMember) {
      return 40
    } else {
      return 50
    }
  } else if (item === "tea") {
    if (isMember) {
      return 30
    } else {
      return 40
    }
  } else {
    return 0
  }
}

getPrice("coffee", true)   // 40
getPrice("coffee", false)  // 50

ch02 Summary

ch03 — HTML

Now we learn what a web page actually is

What is HTML?

HTML is the structure of every web page:

<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <h1>Hello</h1>
    <p>This is a paragraph</p>
  </body>
</html>

Common Elements

Element What it does
<h1> to <h6> Headings (h1 biggest)
<p> Paragraph
<div> Container box
<span> Inline container
<a href="..."> Link
<img src="..."> Image
<button> Button
<input> Text input
<ul> / <li> List

Boxes Inside Boxes

HTML is layers of boxes:

html
├── head
│   └── title
└── body
    ├── h1
    ├── p
    └── div
        └── button

Create Your First HTML File

Create a file called my-page.html:

<!DOCTYPE html>
<html>
<head>
  <title>My Page</title>
</head>
<body>
  <h1>Hello World</h1>
  <p>This is my first web page!</p>
  <button>Click me</button>
</body>
</html>

Open it in your browser. You made a web page!

ch03 Summary

ch04 — CSS Basics

HTML is the structure. CSS makes it look good.

What is CSS?

h1 {
  color: red;
  font-size: 32px;
}

Add CSS to HTML

Add <style> in <head>:

<head>
  <title>My Page</title>
  <style>
    h1 { color: red; }
    p { font-size: 20px; }
    body {
      background: #1a1a2e;
      color: #eee;
      font-family: sans-serif;
    }
  </style>
</head>

Colors

color: red;              /* named */
color: #ff0000;          /* hex */
color: rgb(255, 0, 0);   /* rgb */

Common: red, blue, green, white, black, gray, orange

Try in F12:

document.querySelector("h1").style.color = "red"

Box Model

Every element is a box:

┌──────── margin ────────┐
│ ┌───── border ───────┐ │
│ │ ┌── padding ─────┐ │ │
│ │ │    content      │ │ │
│ │ └────────────────┘ │ │
│ └────────────────────┘ │
└────────────────────────┘
div {
  padding: 10px;
  border: 1px solid white;
  margin: 20px;
}

Style a Button

button {
  font-size: 20px;
  padding: 10px 24px;
  border: 1px solid #eee;
  background: none;
  color: #eee;
  cursor: pointer;
  border-radius: 8px;
}

Copy this into your <style>. The button looks much better now.

ch04 Summary