Skip to content

Commit a291658

Browse files
committed
complete map method
1 parent 7bf5121 commit a291658

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

11_Working_with_Arrays/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@ This method is similar to the `forEach` method but the difference is that the `M
193193
194194
![map](https://user-images.githubusercontent.com/62628408/226436590-6e16b20e-89b4-4e7f-883e-cef00552d67a.png)
195195
196+
```js
197+
const euroToUSD = 1.1;
198+
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
199+
cost movementsUSD = movements.map(function (mov) {
200+
return mov * euroToUSD;
201+
});
202+
```
203+
204+
We can also use the arrow function in the callback function to make it look even neater.
205+
206+
```js
207+
const movementsUSD = movements.map(mov => mov * euroToUSD);
208+
```
209+
210+
The `map()` method also has access to the same parameters, we can also get the `index`, and the whole `array` as well.
211+
212+
```js
213+
const movementsDescriptions = movements.map(
214+
(mov, i) =>
215+
`Movements ${i}: You ${mov > 0 ? 'deposited' : 'withdrew'} ${Math.abs(mov)}`
216+
);
217+
console.log(movementsDescriptions);
218+
```
219+
196220
## Filter Method (Array.prototype.filter())
197221
198222
The `filter()` method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function (a certain condition).

11_Working_with_Arrays/script.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ const displayMovements = function (movements) {
8181
};
8282
displayMovements(account1.movements);
8383

84+
const createUsernames = function (acc) {
85+
acc.forEach(function (acc) {
86+
acc.username = acc.owner
87+
.toLowerCase()
88+
.split(' ')
89+
.map(letter => letter[0])
90+
.join('');
91+
});
92+
};
93+
createUsernames(accounts);
94+
console.log(accounts);
95+
8496
/////////////////////////////////////////////////
8597
/////////////////////////////////////////////////
8698
// LECTURES
@@ -91,7 +103,29 @@ const currencies = new Map([
91103
['GBP', 'Pound sterling'],
92104
]);
93105

94-
// const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
106+
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
107+
108+
const euroToUsd = 1.1;
109+
// const movementsUSD = movements.map(function (mov) {
110+
// return mov * euroToUsd;
111+
// });
112+
113+
// Arrow function
114+
const movementsUSD = movements.map(mov => mov * euroToUsd);
115+
console.log(movements);
116+
console.log(movementsUSD);
117+
118+
// for of loop
119+
const movementsUSDFor = [];
120+
for (const mov of movements) {
121+
movementsUSDFor.push(mov * euroToUsd);
122+
}
123+
124+
const movementsDescriptions = movements.map(
125+
(mov, i) =>
126+
`Movements ${i}: You ${mov > 0 ? 'deposited' : 'withdrew'} ${Math.abs(mov)}`
127+
);
128+
console.log(movementsDescriptions);
95129

96130
/////////////////////////////////////////////////
97131

0 commit comments

Comments
 (0)