-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_practice.js
More file actions
77 lines (56 loc) · 2.03 KB
/
map_practice.js
File metadata and controls
77 lines (56 loc) · 2.03 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
// Lets practice Map method in JS
/*
Task: You need to create an array of objects containing the names of students along with a message indicating whether they passed or failed.
The passing score is 80 or above.
*/
const students = [
{ id: 1, name: 'John', score: 85 },
{ id: 2, name: 'Alice', score: 92 },
{ id: 3, name: 'Bob', score: 78 },
{ id: 4, name: 'Eve', score: 88 },
{ id: 5, name: 'Michael', score: 95 }
];
// Solution:1
// This is the solution of this problem using simple if else condition but it didn't give u array of object it will give you array
let passStudents = students.map((items) => {
if (items.score > 80) {
return `${items.name} Congratulations you have pass!`;
} else if (items.score < 80) {
return `${items.name} Sorry you are fail!`
}
})
// console.log(passStudents);
// Solution:2
// Lets solve this and return the value in the form of array of objects
let passOrFailed = students.map((student) => {
return {
name: student.name,
result : student.score > 80 ? 'Passed' : 'Failed'
}
})
// console.log(passOrFailed);
/*
Task: You need to create an array of strings containing the names of the products along with their prices formatted as a currency string.
*/
const products = [
{ id: 1, name: 'Laptop', price: 999 },
{ id: 2, name: 'Smartphone', price: 699 },
{ id: 3, name: 'Tablet', price: 399 },
{ id: 4, name: 'Headphones', price: 149 },
{ id: 5, name: 'Smartwatch', price: 249 }
];
// Solution
let productsPrices = products.map((prodcut) => {
return `${prodcut.name} Pice is $:${prodcut.price}`
})
// console.log(productsPrices);
const myNums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Task: Multiply all items of array with 10;
// Task: Add 1 in all items of array;
// filter out all items > 40
let newItems = myNums
.map(num => num * 10)
.map(num => num + 1)
.filter(num => num > 40);
console.log(newItems);
// This is called chaning where we use mutiple methods at once in JS