1 of 40

Announcements

  • 📝Hw0 (Setup): weblab.is/hw0
    • Need to set up MongoDB by Thursday
  • 📗 Panopto livestream is working now! weblab.is/livestream (and we still have weblab.is/zoom )
  • 🧠No office hours today
  • 🪧Milestone 0 (find a team and brainstorm 10 ideas) due eod Wednesday
  • 💛Still need a team? Check out the team finding piazza post
  • 🎦Recordings from today will be available as soon as we edit + post them (hopefully this evening). Slides are all available at weblab.is/schedule and weblab.is/slides

2 of 40

JS Cont: Callback Functions and Arrays

Abby Chou

3 of 40

{ const tempFahrenheit = tempCelsius * 9/5 + 32;

return tempFahrenheit; };

(tempCelsius)

=>

4 of 40

{ const tempFahrenheit = tempCelsius * 9/5 + 32;

return tempFahrenheit; };

(tempCelsius)

=>

celsiusToFahrenheit

5 of 40

{ const tempFahrenheit = tempCelsius * 9/5 + 32;

return tempFahrenheit; };

(tempCelsius)

=>

celsiusToFahrenheit

6 of 40

{ const tempFahrenheit = tempCelsius * 9/5 + 32;

return tempFahrenheit; };

(tempCelsius)

=>

celsiusToFahrenheit

7 of 40

Review: Push and Pop

8 of 40

Review: Looping Through an Array

9 of 40

Practice!

Write a function that takes in an array of temperatures (in Celsius) and outputs an array with all the temperatures converted to Fahrenheit.

DO NOT mutate (change) the input array!

Paste your code into the browser console (or write it directly there) to test.

tempF = tempC * 9/5 + 32

10 of 40

Adding more…

Let’s say we now want three functions:

  • Converting an array of Celsius temps to Fahrenheit (what we just did)
  • Converting an array of Fahrenheit temps to Celsius
  • Converting an array of Celsius temps to Kelvin

11 of 40

Adding more…

Let’s say we now want three functions:

  • Converting an array of Celsius temps to Fahrenheit (what we just did)
  • Converting an array of Fahrenheit temps to Celsius
  • Converting an array of Celsius temps to Kelvin

12 of 40

We want to reuse all this code *except* the the part highlighted in orange.

13 of 40

Callback functions to the rescue!

Callback functions

14 of 40

Using Callbacks

Step 1: Write a general modifyArray function that takes in 2 inputs: an array and a callback function, and returns a new array, the result of applying the callback function to each element of the original.

15 of 40

const modifyArray = (arr, transformFunc) => {

// apply transformFunc to all elements of arr, return

// the result

};

16 of 40

const modifyArray = (arr, transformFunc) => {

// apply transformFunc to all elements of arr, return

// the result

};

modifyArray( , );

,

[

]

,

,

hattify

cat

cat with hat

17 of 40

const modifyArray = (arr, transformFunc) => {

// apply transformFunc to all elements of arr, return

// the result

};

modifyArray( , );

,

[

]

,

,

hattify

cat

cat with hat

hattify

[

]

18 of 40

const modifyArray = (arr, transformFunc) => {

// apply transformFunc to all elements of arr, return

// the result

};

modifyArray( , );

,

[

]

,

,

hattify

cat

cat with hat

hattify

,

[

]

19 of 40

const modifyArray = (arr, transformFunc) => {

// apply transformFunc to all elements of arr, return

// the result

};

modifyArray( , );

,

[

]

,

,

hattify

cat

cat with hat

hattify

,

[

]

,

20 of 40

const modifyArray = (arr, transformFunc) => {

// apply transformFunc to all elements of arr, return

// the result

};

modifyArray( , );

,

[

]

,

,

hattify

cat

cat with hat

hattify

,

[

]

,

,

21 of 40

const modifyArray = (arr, transformFunc) => {

// apply transformFunc to all elements of arr, return

// the result

};

modifyArray( , );

,

[

]

,

,

hattify

cat

cat with hat

,

[

]

,

,

22 of 40

Let’s generalize :D

Step 1: Write a general modifyArray function that takes in 2 inputs: an array and a callback function, and returns a new array, the result of applying the callback function to each element of the original.

Step 2: Use your modifyArray function to do the following:

  • Convert an array of Celsius temps to Fahrenheit (what we just did)
  • Convert an array of Fahrenheit temps to Celsius
    • tempC = (tempF - 32) * 5/9
  • Convert an array of Celsius temps to Kelvin
    • tempK = tempC + 273.15

23 of 40

Solution

24 of 40

Solution

25 of 40

Side Note: Shortened Function Notation

2 ways of writing functions:

(inputs) => { do stuff (and optionally return an output) }

(inputs) => (output)

This is just a shortened way of writing this!

26 of 40

map(...)

Creates a new array by applying the callback function to every element of the starting array.

27 of 40

Map Practice

This is an array of JavaScript objects!

Copy this array into your JS file, then use map to create an array of the areas of the rectangles.

Map example:

Objects example:

28 of 40

Solution

29 of 40

filter(...)

Creates a new array by selecting the elements in the starting array which pass the given “test” (i.e. filtering out the “bad” elements and keeping the “good” ones).

30 of 40

Filter Practice

Using the rectangles array from earlier, use filter to create an array that only contains rectangles with perimeter > 10.

Example:

31 of 40

Solution

32 of 40

Why do we use callback functions?

  1. Reusability (map and filter)
    1. So we don’t have to write code to loop through an array over and over.
  2. Abstraction
    • “When x happens, do this”

We can write the code for this…

And let other smart ppl (the writers of JavaScript libraries) write the code for this!

33 of 40

Callback Functions in Web Dev

Let’s say we wrote a function, updateAnimation(), that we want to call every 10 milliseconds.

Writing code to manage timers is annoying, so the JavaScript authors did it for us!

“When the timer goes off, call updateAnimation”

34 of 40

Callback Functions in Web Dev

Snake Workshop: Responding to keyboard inputs!

35 of 40

Callback Functions in Web Dev

Snake Workshop: Responding to keyboard inputs!

“When you get a keypress, do this with it”

36 of 40

Callback Functions in Web Dev

Later: Database query returns a promise, and we want to do something when that promise resolves (i.e. when the database gives us the info we need)

37 of 40

Callback Functions in Web Dev

Later: Database query returns a promise, and we want to do something when that promise resolves (i.e. when the database gives us the info we need)

38 of 40

Callback Functions in Web Dev

When we receive an http GET request to the /comment endpoint, we want to send a response with the comments we got from the database.

39 of 40

Callback Functions in Web Dev

When we receive an http GET request to the /comment endpoint, we want to send a response with the comments we got from the database.

40 of 40

Feedback Time!

weblab.is/feedback