Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 656fd64

Browse files
committed
js functions
1 parent 17c52c6 commit 656fd64

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

units/03/js-functions.markdown

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
layout: post
33
title: "Exercise: Javascript Functions"
4-
nexttitle: 3.3
5-
nexturl: units/03/js-functions
6-
prevurl: units/02/lab
4+
nexttitle: 3.3 Typescript
5+
nexturl: units/03/js-typescript
6+
prevurl: units/03/js-types
77
prevtitle: 3.1 JS Types
88
---
99

@@ -49,11 +49,9 @@ const foo = function(x) { return x + 1; }
4949
const foo = (x) => x + 1;
5050
```
5151

52-
The only difference is that in arrow functions, the keyword `this` refers to the **parent scope**.
52+
## Callback Functions
5353

54-
## Callback Functions
55-
56-
A **callback function** is a function that is passed into another function as a parameter. For example, setTimeout takes a callback function that will execute after 1000 ms, written using an arrow function or in the regular function notation:
54+
A **callback function** is a function that is passed into another function as a parameter. For example, `setTimeout` takes a callback function that will execute after 1000 ms, written using an arrow function or in the regular function notation:
5755

5856
```js
5957
// Snippet 1 - regular function notation
@@ -65,16 +63,38 @@ setTimeout(function() {
6563
setTimeout(() => console.log("Hello!"), 1000);
6664
```
6765

68-
When a function is passed into another function without being named, it is called an **anonymous function**. Both the `console.log` functions in the above snippets are anonymous functions. You'll see these a lot in javascript code, both in front end and back end development.
66+
When a function is passed into another function without being named, it is called an **anonymous function**. Both the functions that `console.log` in the above snippets are anonymous functions. You'll see these *a lot* in javascript code, both in front end and back end development.
6967

7068
## Exercises
7169

7270
1. Write an arrow function that takes a string and returns the lowercase version of that string.
73-
2. Write an arrow function that takes a string `prefix` and returns another function that prepends `prefix` to its parameter.
71+
2. Write an arrow function that takes a string `prefix` and returns a **function** that prepends `prefix` to its own parameter.
72+
73+
```js
74+
const prefix = "Hello, ";
75+
const funcPrepend = (prefix) => {
76+
// Your code here
77+
}
78+
// funcPrepend("hello") -> function that takes a string)
79+
// funcPrepend("hello")("world") -> "hello world"
80+
```
7481
3. Write an arrow function that takes a string `prefix` and returns a **function** that takes a string `postfix` that returns another function that takes a string and returns correctly prepends/appends the pre/postfix to its parameter.
75-
These practice questions are taken from [eloquentjavascript.net](https://eloquentjavascript.net/05_higher_order.html).
82+
83+
```js
84+
const prefix = "Hello, ";
85+
const postfix = "!";
86+
const funcPrepend = (prefix) => {
87+
// Your code here
88+
}
89+
// funcPrepend("hello") -> function that takes a string)
90+
// funcPrepend("hello")("foo") -> function that takes a string
91+
// funcPrepend("hello")("goodbye")("foo") -> "hello foo goodbye!"
92+
```
93+
This was just showing you that you can return functions from functions.
94+
95+
Some of these practice questions are taken from [eloquentjavascript.net](https://eloquentjavascript.net/05_higher_order.html).
7696

77-
1. Write a function that maps number grades to letter grades. Use the following grading scale:
97+
1. Write a function that maps (use `map()`) number grades to letter grades. Use the following grading scale:
7898
```
7999
A is 89.5-100, B is [79.5-89.5), C is [69.5-79.5), D is [59.5-69.5), and F is [0-59.49).
80100
```

0 commit comments

Comments
 (0)