#100Devs - Array Review & Javascript Objects (cohort 2)

Class 20 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 79

 

Javascript - Arrays & Objects

Leon Noel

The money turned my noodles into pasta
The money turned my tuna into lobster 

#100Devs


Slide 2 of 79

Agenda


Slide 3 of 79

Questions

About last class or life


Slide 4 of 79

Checking In

Like and Retweet the Tweet

!checkin


Slide 5 of 79

Little bit more time


Slide 6 of 79

Want to be fancy?

Best Clip Get's Special Color On Discord!


Slide 7 of 79

Networking

3 Individuals Already In Tech

2 Coffee Chats


Slide 8 of 79

USE THE SHEET!

Networking Google Sheet


Slide 9 of 79

Coding Challenges

Daily


Slide 10 of 79

Paid Client

Due by Mar. 31st


Slide 11 of 79

Live Portfolio & Resume Review

https://forms.gle/VkbSwgnyRo6VkTFL8


Slide 12 of 79

TURN IT UP!


Slide 13 of 79

Research


Slide 14 of 79

Trough Of Sorrow


Slide 15 of 79

Don't feel bad for being exactly where you should be...

*Thanks Blaw


Slide 16 of 79

Programming


Slide 17 of 79

A computer will do what you tell it to do.


Slide 18 of 79

What is a program?

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


Slide 19 of 79

What is a programming?

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


Slide 20 of 79

JAVASCRIPT


Slide 21 of 79

Has a specific 'Syntax'

Syntax: "Spelling and grammar" rules of a programming language.


Slide 22 of 79

Variables

Declaration: let age

 

Assignment: age = 25

 

Both at the same time:

let age = 25


Slide 23 of 79

Conditional Syntax

if(condition is true) {
  //Do this cool stuff
}else if(condition is true){
  //Do this other cool stuff
}else{
  //Default cool stuff
}

Slide 24 of 79

🚨 DANGER 🚨

Assignment vs. Comparison


Slide 25 of 79

Multiple Conditions

if (name === "Leon" && status === "Ballin"){
 //Wink at camera
}

Slide 26 of 79

Multiple Conditions

if (day === "Saturday" || day === "Sunday"){
  //It is the weekend
}

Slide 27 of 79

Functions

What are functions?


Slide 28 of 79

Functions


Slide 29 of 79

Functions

function name(parameters){
  //body
}

//call
name(arguments)

Slide 30 of 79

Functions

function yell(word){
  alert(word)
}


yell("HELLO")

Slide 31 of 79

Loops

What are loops?


Slide 32 of 79

Loops


Slide 33 of 79

Loops - For

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

Slide 34 of 79

Loops - While

let count = 0 

while(count < 5){
  console.log(count)
  count++
}

Slide 35 of 79

Let's Code

Review - In Paradise


Slide 36 of 79

Arrays

What are arrays?


Slide 37 of 79

Toasters


Slide 38 of 79

Arrays


Slide 39 of 79

Declaring Arrays

let newArr = new Array()

Array Constructor


Slide 40 of 79

Declaring Arrays

let newArr = []

Literal Notation


Slide 41 of 79

Declaring Arrays

newArr = ['Zebra',true,21]

Arrays are populated with elements

Elements can be of any type


Slide 42 of 79

Array Indexing


Slide 43 of 79

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 44 of 79

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 45 of 79

Array Indexing

let cars = ['Honda', 'Toyota', 'Ford', 'Tesla']
let nums = [1,2,3]
cars = nums
console.log( cars ) //[1,2,3]

You can overwrite whole arrays by assigning an array to a different array


Slide 46 of 79

Array Length

console.log( newArr.length ) //4

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


Slide 47 of 79

Let's Code

In Paradise: After Paradise


Slide 48 of 79

Array Iteration

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

for(let i = 0; i < bestColors.length;i++){
  console.log( bestColors[i] )
}

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


Slide 49 of 79

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 50 of 79

Let's Code

Summer Games


Slide 51 of 79

Other Arrays Methods

let bestRappers2020 = ['6ix9ine','Polo G','6ix9ine']

let removed = bestRappers2020.shift()

console.log( bestRappers2020 ) // ['Polo G', '6ix9ine']

Remove item from the beginning of an Array


Slide 52 of 79

Other Arrays Methods

let bestRappers2020 = ['Polo G','6ix9ine']

let removedAgain = bestRappers2020.pop()

console.log( bestRappers2020 ) // ['Polo G']

Remove item from the end of an Array


Slide 53 of 79

Other Arrays Methods

let bestRappers2020 = ['Polo G']

bestRappers2020.unshift('Dylan')

console.log( bestRappers2020 ) // ['Dylan','Polo G']

Add item to the beginning of an Array


Slide 54 of 79

Other Arrays Methods

let bestRappers2020 = ['Dylan','Polo G']

bestRappers2020.push('Dylan')

console.log( bestRappers2020 ) // ['Dylan','Polo G','Dylan']

Add item to the end of an Array


Slide 55 of 79

Other Arrays Methods

let bestRappers2020 = ['Dylan','Polo G','Dylan']

let bestRappersAllTime = bestRappers2020.map(x => 'Dylan')

bestRappersAllTime.unshift('Dylan')
  
bestRappersAllTime.push('Dylan')

console.log( bestRappersAllTime ) 

// ['Dylan','Dylan','Dylan', 'Dylan', 'Dylan']

Slide 56 of 79

Let's Code

Winter Games


Slide 57 of 79

Objects

What are objects?


Slide 58 of 79

Objects


Slide 59 of 79

Think of a physical object

What are it's attributes and behaviors? 


Slide 60 of 79

How about a stopwatch

What are its attributes and behaviors? 


Slide 61 of 79

Stopwatch Object

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

 

 


Slide 62 of 79

Stopwatch Object

let stopwatch = {}

stopwatch.currentTime = 12

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

stopwatch.tellTime(stopwatch.currentTime)

Slide 63 of 79

Let's Code

Objects - Listen To Your Heart


Slide 64 of 79

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 65 of 79

Car Factory?

Constructors then Classes


Slide 66 of 79

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 67 of 79

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 68 of 79

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 69 of 79

Let's Code

Objects - Street Fighter


Slide 70 of 79

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 71 of 79

APIs

What are APIs?


Slide 72 of 79

APIs


Slide 73 of 79

APIs


Slide 74 of 79

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 75 of 79

Let's Code

DOG PHOTOS!


Slide 76 of 79

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 77 of 79

Let's Code

EVERYBODY! SHOTS! SHOTS! SHOTS!


Slide 78 of 79

Let's Code

NASA PHOTOS


Slide 79 of 79

Homework

Read: Ahead on slides and problems

Read: https://javascript.info/array-methods

Research: Array Methods

Do: JS30 Day 04 Array Cardio