Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move arrow functions concept to different exercise #1844

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,8 @@
"slug": "elyses-analytic-enchantments",
"name": "Elyses Analytic Enchantments",
"uuid": "45d956db-d4ef-4468-b1d3-47021f172c15",
"concepts": ["array-analysis"],
"prerequisites": [
"arrays",
"arrow-functions",
"booleans",
"callbacks",
"numbers"
],
"concepts": ["arrow-functions", "array-analysis"],
"prerequisites": ["arrays", "booleans", "callbacks", "numbers"],
"status": "beta"
},
{
Expand Down Expand Up @@ -181,7 +175,7 @@
"slug": "fruit-picker",
"name": "Fruit Picker",
"uuid": "a6348db8-cc2b-4c53-9f43-3c23248d66f0",
"concepts": ["arrow-functions", "callbacks"],
"concepts": ["callbacks"],
"prerequisites": ["functions", "objects"],
"status": "beta"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
# Introduction

## Arrow Functions

Besides function declarations and function expressions, JavaScript also has another very concise syntax for defining a function.
These functions are called _arrow functions_.

Here is a comparison between a function declaration and an arrow function.

```javascript
function addUpTwoNumbers(num1, num2) {
return num1 + num2;
}
// function keyword removed and => added
const addUpTwoNumbers = (num1, num2) => {
return num1 + num2;
};
```

If the function body contains only a return statement, like in the example above, the `{}` and the `return` keyword can be omitted.
If there is only one parameter, the parenthesis `()` can be omitted as well.

<!-- prettier-ignore-start -->
```javascript
const addUpTwoNumbers = (num1, num2) => num1 + num2;
const square = num => num * num;
```
<!-- prettier-ignore-end -->

Arrow functions are often used to define short callback functions directly in the function call.

<!-- prettier-ignore-start -->
```javascript
applyToSquare(number => number * number);
```
<!-- prettier-ignore-end -->

## Array Analysis

[Arrays][arrays-concept] have built-in methods to analyse the contents of the array.
Most of these methods take a function that returns true or false as an argument.
Such a function is called a [`predicate`][predicate_in_programming].
Expand Down Expand Up @@ -28,7 +65,7 @@ numbers.indexOf('two');

Some other helpful built-in methods that are available to analyze an array are shown below. See [MDN][mdn-array-methods] for a full list of array methods.

## `includes`
### `includes`

> The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. [^1]

Expand All @@ -40,7 +77,7 @@ numbers.includes('one');
// => false
```

## `every`
### `every`

> The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value. [^2]

Expand All @@ -50,7 +87,7 @@ numbers.every((num) => num % 2 !== 0);
// => true
```

## `some`
### `some`

> The some() method tests whether at least one element in the array passes the test implemented by the provided function. [^3]

Expand All @@ -60,7 +97,7 @@ numbers.some((num) => num % 2 !== 0);
// => true
```

## `find`
### `find`

> The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. [^4]

Expand All @@ -70,7 +107,7 @@ numbers.find((num) => num < 5);
// => 1
```

## `findIndex`
### `findIndex`

> The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test. [^5]

Expand Down