#100Devs - More API Review

Class 27 of our Free Web Dev Bootcamp for folx affected by the pandemic. Join live T/Th 6:30pm ET leonnoel.com/twitch and ask questions here: leonnoel.com/discord

Skip to first slide
Slide 1 of 56

 

Javascript - More API Review

Leon Noel

#100Devs

Hand me downs with the patches
Mama put a little money in the mattress
Taught me how to make a silver spoon out of plastic 


Slide 2 of 56

Agenda


Slide 3 of 56

Questions

About last class or life


Slide 4 of 56

Checking In

Like and Retweet the Tweet

!checkin


Slide 5 of 56

You Are Not Alone!


Slide 6 of 56

Health First


Slide 7 of 56

No Networking Until May


Slide 8 of 56

Client Deadline: May 3rd


Slide 9 of 56

Client Alternatives


Slide 10 of 56

Grassroots Volunteer*


Slide 11 of 56

Free Software / Open Source

https://www.firsttimersonly.com/


Slide 12 of 56

Codewars !Clan

Clan: #100Devs - leonnoel.com/twitch


Slide 13 of 56

Programming


Slide 14 of 56

What is a program?

A program is a set of instructions that you write to tell a computer what to do


Slide 15 of 56

What is a programming?

Programming is the task of writing those instructions in a language that the computer can understand.


Slide 16 of 56

JAVASCRIPT


Slide 17 of 56

Loops

What are loops?


Slide 18 of 56

Loops


Slide 19 of 56

Loops - For

for (let i = 1; i < 5; i++) {
  console.log(i)
}

Slide 20 of 56

Arrays

What are arrays?


Slide 21 of 56

Toasters


Slide 22 of 56

Arrays


Slide 23 of 56

Declaring Arrays

let newArr = []

Literal Notation


Slide 24 of 56

Declaring Arrays

newArr = ['Zebra',true,21]

Arrays are populated with elements

Elements can be of any type


Slide 25 of 56

Array Indexing


Slide 26 of 56

Array Indexing

newArr = ['Zebra',,true,21]

console.log( newArr[0] )  //Zebra
console.log( newArr[1] )  //undefined
console.log( newArr[2] )  //true
console.log( newArr[3] )  //21

Elements can be accessed by their index numbers


Slide 27 of 56

Array Indexing

newArr = ['Zebra',,true,21]

newArr[1] = 'Bob'

console.log( newArr )  

// ['Zebra','Bob',true,21]

Elements can be updated by using the index at that position


Slide 28 of 56

Array Length

console.log( newArr.length ) //4

How do you determine how many elements are in your array?


Slide 29 of 56

Array Iteration

let bestColors = ['green','blue','yellow','black']

bestColors.forEach((x,i)=> console.log(x))

Iterates through an array passing in the value and index of each element 


Slide 30 of 56

Objects

What are objects?


Slide 31 of 56

Objects


Slide 32 of 56

Think of a physical object

What are it's attributes and behaviors? 


Slide 33 of 56

Stopwatch Object

  • Properties (attributes):
    Might contain a variable to represent hours, another to represent minutes, and another to represent seconds. 

 

 


Slide 34 of 56

Stopwatch Object

let stopwatch = {}

stopwatch.currentTime = 12

stopwatch.tellTime = function(time){
  console.log(`The current time is ${time}.`)
}

stopwatch.tellTime(stopwatch.currentTime)

Slide 35 of 56

Objects

What if we want to make

a lot of objects?

How much money you got? How many problems you got? How many people done doubted you? Left you out to rot?


Slide 36 of 56

Car Factory?

Constructors then Classes


Slide 37 of 56

Car Factory

Constructor

function MakeCar(carMake,carModel,carColor,numOfDoors){
  this.make = carMake
  this.model = carModel
  this.color = carColor
  this.doors = numOfDoors
  this.honk = function(){
    alert('BEEP BEEP FUCKER')
  }
  this.lock = function(){
    alert(`Locked ${this.doors} doors!`)
  }
}

let hondaCivic = new MakeCar('Honda','Civic','Silver', 4)

let teslaRoadster = new MakeCar('Tesla','Roadster', 'Red', 2)

Slide 38 of 56

Car Factory

We forgot enable bluetooth! 

let teslaRoadster = new MakeCar('Tesla','Roadster', 'Red', 2)

console.log( teslaRoadster.bluetooth )  //undefined

MakeCar.prototype.bluetooth = true

console.log( teslaRoadster.bluetooth ) //true 

A prototype is another object that is used as a fallback source of properties


Slide 39 of 56

Car Factory

Why does .toString() work?!?

let teslaRoadster = new MakeCar('Tesla','Roadster', 'Red', 2)

console.log( teslaRoadster.doors.toString() )  // "2" not 2

A prototype is another object that is used as a fallback source of properties


Slide 40 of 56

Car Factory

Look Ma! New syntax!

class MakeCar{
  constructor(carMake,carModel,carColor,numOfDoors){
    this.make = carMake
    this.model = carModel
    this.color = carColor
    this.doors = numOfDoors
  }
  honk(){
    alert('BEEP BEEP FUCKER')
  }
  lock(){
    alert(`Locked ${this.doors} doors!`)
  }
}

let hondaCivic = new MakeCar('Honda','Civic','Silver', 4)

let teslaRoadster = new MakeCar('Tesla','Roadster', 'Red', 2)

Classes are like templates for objects!


Slide 41 of 56

Let's Code

Objects - Netflix TV Shows


Slide 42 of 56

APIs

What are APIs?


Slide 43 of 56

APIs


Slide 44 of 56

APIs


Slide 45 of 56

APIs

Fetch Fido, Fetch!

fetch("https://dog.ceo/api/breeds/image/random")
    .then(res => res.json()) // parse response as JSON
    .then(data => {
      console.log(data)
    })
    .catch(err => {
        console.log(`error ${err}`)
    });

API returns a JSON object that we can use within our apps


Slide 46 of 56

APIs

Stop trying to make Fetch happen!

fetch(url)
    .then(res => res.json()) // parse response as JSON
    .then(data => {
      console.log(data)
    })
    .catch(err => {
        console.log(`error ${err}`)
    });

Some APIs need Query Parameters to return the correct data

const url = 'https://www.thecocktaildb.com/api/json/v1/1/search.php?s=margarita'


Slide 47 of 56

Let's Code

NASA PHOTOS


Slide 48 of 56

Local Storage


Slide 49 of 56

Local Storage

Allows you to store data across browser sessions


Slide 50 of 56

Put Item Into Local Storage

localStorage.setItem('bestFriend', 'Bob')

Slide 51 of 56

Get Item Out Of Local Storage

localStorage.getItem('bestFriend', 'Bob')

Slide 52 of 56

Remove Item In Local Storage

localStorage.removeItem('bestFriend', 'Bob')

Slide 53 of 56

Remove All In Local Storage

localStorage.clear()

Slide 54 of 56

Let's Code

Local botScore Button


Slide 55 of 56

Let's Code

A Card Game


Slide 56 of 56

Let's Code

A Book Tracker