-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.js
136 lines (104 loc) · 2.67 KB
/
day9.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
for(let i=0;i<5;i++){
console.log(i)
}
const arr=[11,22,33,44,55]
for (let k of arr){
console.log(k)
}
const arr1=[34,35,36,37]
for (let s in arr1){
console.log(s)
}
//map
let obj1={
name:"karan",
name1:"karan1",
name2:"karan2",
name3:"karan3"
}
// for (const [key,value] of obj1){
// console.log(key,"==",value)
// }
// object cannot be iterable with for loop here => gives an error
for (const key in obj1){
console.log(key)
console.log(obj1[key])
}
const arr26=[19,29,39,49,59,69]
arr26.forEach(function (item) {
console.log(item)
})
arr26.forEach( (item)=>{
console.log(item)
})
const mynums=[90,80,70,60,40,30,20]
const newnum=[]
mynums.forEach( (num)=>{
if (num>40){
newnum.push(num)
}
})
console.log(newnum)
//exe
const books=[
{title:"book1", genre :"crime", publish:1981 , edition:2004 },
{title:"book1", genre :"fiction",publish:1985,edition:2004},
{title:"book1", genre :"romance",publish:1989,edition:2004},
{title:"book1", genre :"fiction",publish:1999,edition:2004},
{title:"book1", genre :"action",publish:1909,edition:2004},
{title:"book1", genre :"history",publish:2003,edition:2004}
];
let userbooks=books.filter( (obj)=>obj.genre=="fiction")
console.log(userbooks)
userbooks=books.filter((obj)=>obj.publish>=1998)
console.log(userbooks)
userbooks=books.filter((obj)=>{
return obj.publish>=2000 && obj.genre=="history"
})
console.log(userbooks)
//map
let numerics=[1,2,3,4,5,6,7,8,9,10]
const newnumeric= numerics.map( (value1)=>value1+10)
console.log(newnumeric)
let numerics1=[1,2,3,4,5,6,7,8,9,10]
const changing=numerics1.map((yenum)=>yenum*10).map((yenum)=>yenum+5)
console.log(changing)
let numerics11=[1,2,3,4,5,6,7,8,9,10]
const changing1=numerics1.map((yenum1)=>yenum1*10).map((yenum1)=>yenum1+5).filter((yenum)=>yenum>=80)
console.log(changing1)
const newnumeric1= numerics1.map( (value11)=>{ return value11+10})
console.log(newnumeric1)
//reduce
const myreduce=[2,4,6,8,10,12]
const mytotal=myreduce.reduce(function (acc,currvalue) {
console.log(`acc value is ${acc} and value of curr value ${currvalue}`)
return acc+currvalue
},acc=0)
console.log(mytotal)
const myreduce1=[2,4,6,8,10,12]
const mytotal1=myreduce.reduce((acc,curr)=> acc+curr,0)
console.log(mytotal1);
const shoppingcart=[
{
item:"js course",
price:999
},
{
item:"python",
price:10000
},
{
item:"java course",
price:12000
},
{
item:"data science course",
price:20000
},
{
item:"css",
price:5000
}
];
const mybill=shoppingcart.reduce((acc,curr)=>acc+curr.price,0)
console.log(mybill)