mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Clement Denis
d3a8b56646
|
4 years ago | |
---|---|---|
.. | ||
README.md | 4 years ago |
README.md
Squared
.filter
is not the only useful array method that do loops for you.
map
The .map
method is another very powerful tool once mastered, let's see it in
action:
const time10 = [1, 2, 3, 4, 5].map((num) => {
return `#${num}`
})
console.log(time10) // [`#1`, `#2`, `#3`, `#4`, `#5`]
Map takes a function and apply it to each elements of the array.
Note that map will never change the number of element of the array
For example if your function return nothing:
const nothingX3 = [1, 2, 3].map((num) => {
// Not doing anything today...
})
console.log(nothingX3) // [undefined, undefined, undefined]
We still get an array of 3 elements, but they are undefined
.
You should use map everytime you want to repeat the same action for all elements.
Instructions
Declare a function toSquares
that takes an array of numbers and return an
array of those squared numbers
Example:
const result = toSquares([1, 2, 3, 4])
console.log(result) // [1, 4, 9, 16]