Arrow functions application in JavaScript
Arrow functions have a particular advantage
as callbacks. Let's look at an example of
how much the code is simplified in this
case. For example, let's say we have the following
filter function with callback:
javascriptlet result = filter([1, 2, 3, 4, 5], function(elem) {
if (elem % 2 === 0) {
return true;
} else {
return false;
}
});
First, let's get rid of the construction
if and write the condition simply
through the operator ===:
javascriptlet result = filter([1, 2, 3, 4, 5], function(elem) {
return elem % 2 == 0;
});
Let us now replace the ordinary function with an arrow one:
javascriptlet result = filter([1, 2, 3, 4, 5], elem => elem % 2 == 0);
Задание 1
Given the following function with a callback:
javascriptlet result = every([1, 2, 3, 4, 5], function(elem) {
if (elem > 0) {
return true;
} else {
return false;
}
});
Simplify the callback with an arrow function.
Задание 2
Given the following function with a callback:
javascriptlet result = every([1, 2, 3, 4, 5], function(elem, index) {
if (elem * index > 10) {
return true;
} else {
return false;
}
});
Simplify the callback with an arrow function.
Задание 3
Given the following function with a callback:
javascriptlet arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
let result = each(arr, function(elem, index) {
if (elem * index > 10) {
return true;
} else {
return false;
}
});
Simplify the callback with an arrow function.