-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIteratorMethod.txt
More file actions
81 lines (54 loc) · 1.48 KB
/
Copy pathArrayIteratorMethod.txt
File metadata and controls
81 lines (54 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Task 1: Using forEach()
const cities = ["Tokyo", "Paris", "New York", "Kyoto", "Rome"];
cities.forEach(city => {
console.log(city.toUpperCase());
});
/* Expected Output:
TOKYO
PARIS
NEW YORK
KYOTO
ROME
*/
// Task 2: Transforming with map()
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map(num => num ** 2);
console.log(squares);
/* Expected Output:
[1, 4, 9, 16, 25]
*/
// Task 3: Filtering with filter()
const scores = [85, 42, 90, 75, 30, 100];
const highScores = scores.filter(score => score >= 80);
console.log(highScores);
/* Expected Output:
[85, 90, 100]
*/
// Task 4: Finding with find() and findIndex()
const favoriteFood = ["Pho", "Tacos", "Sushi", "Burger", "Pasta"];
const longNameFood = favoriteFood.find(food => food.length > 4);
const longNameFoodIndex = favoriteFood.findIndex(food => food.length > 4);
console.log(longNameFood);
console.log(longNameFoodIndex);
/* Expected Output:
Tacos
1
*/
// Task 5: Checking conditions with some() and every()
const temperatures = [72, 85, 91, 68, 77];
const hasHotTemp = temperatures.some(temp => temp > 90);
const allMildTemp = temperatures.every(temp => temp > 50);
console.log([hasHotTemp, allMildTemp]);
/* Expected Output:
[true, true]
*/
// Task 6: Reducing with reduce()
const totalBudget = 200;
const prices = [45, 30, 60, 25];
const remainingBudget = prices.reduce((accumulator, currentPrice) => {
return accumulator - currentPrice;
}, totalBudget);
console.log(remainingBudget);
/* Expected Output:
40
*/