#100Devs - Javascript APIs Fun Time

Class 26 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 39

 

Javascript - API Fun

Leon Noel

#100Devs

"I'm not a magician, but I'll mystify yah.

I'm super hot, but you can call me mister fire.

If you never seen me battle, well then you missed the fire."


Slide 2 of 39

Agenda


Slide 3 of 39

Questions

About last class or life


Slide 4 of 39

Checking In

Like and Retweet the Tweet

!checkin


Slide 5 of 39

No Networking Until May


Slide 6 of 39

Client Deadline: May 3rd


Slide 7 of 39

Client Alternatives


Slide 8 of 39

Grassroots Volunteer*


Slide 9 of 39

Free Software / Open Source

https://www.firsttimersonly.com/


Slide 10 of 39

!youtube


Slide 11 of 39

!Merch


Slide 12 of 39

THANK YOU


Slide 13 of 39

Programming


Slide 14 of 39

What is a program?

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


Slide 15 of 39

What is a programming?

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


Slide 16 of 39

JAVASCRIPT


Slide 17 of 39

Objects

What are objects?


Slide 18 of 39

Objects


Slide 19 of 39

Think of a physical object

What are it's attributes and behaviors? 


Slide 20 of 39

How about a stopwatch

What are its attributes and behaviors? 


Slide 21 of 39

Stopwatch Object

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

 

 


Slide 22 of 39

Stopwatch Object

let stopwatch = {}

stopwatch.currentTime = 12

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

stopwatch.tellTime(stopwatch.currentTime)

Slide 23 of 39

Let's Code

Objects - Lost Galaxy


Slide 24 of 39

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 25 of 39

Car Factory?

Constructors then Classes


Slide 26 of 39

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 27 of 39

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 28 of 39

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 29 of 39

Let's Code

Objects - Chats Choice


Slide 30 of 39

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 31 of 39

APIs

What are APIs?


Slide 32 of 39

APIs


Slide 33 of 39

APIs


Slide 34 of 39

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 35 of 39

Let's Code

DOG PHOTOS!


Slide 36 of 39

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 37 of 39

Let's Code

EVERYBODY! SHOTS! SHOTS! SHOTS!


Slide 38 of 39

Let's Code

NASA PHOTOS


Slide 39 of 39

Homework

Do: Make the cocktailDB api work with spaces between the names!
Do: Find three APIs and build three simple apps using those APIs (Not all of these work, but it is a start: https://github.com/public-apis/public-apis)
Do: Codewars Daily!
Do: Anki Daily!