-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.js
116 lines (102 loc) · 2.02 KB
/
day15.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
function add1(num1){
return function (num2){
return num1+num2
}
}
const fxn=add1(2)
const ans=fxn(3)
console.log(ans)
function createCounter(){
let counter=0;
return {
increment: ()=> counter++,
getValue: ()=>{
console.log(`Counter: ${counter}`)
}
}
}
const counter= createCounter()
counter.increment()
counter.increment()
counter.getValue()
function uniqueID(){
let id;
return function (){
id=Math.floor(Math.random()*10+1)
return id
}
}
const myID=uniqueID();
console.log(myID())
console.log(myID())
console.log(myID())
function user(name){
let username=name
return function(){
return `Name: ${username}`
}
}
const myUser=user("Chandan")
console.log(myUser())
const functionArray=[]
for(let i=0;i<10;i++){
functionArray.push(()=>{
console.log(i)
})
}
functionArray.forEach(fn=>fn())
function collection(){
const st= new Set()
function add1(ele){
st.add(ele)
}
function remove(ele){
st.delete(ele)
}
function listItems(){
st.forEach(ele=>{
console.log(ele)
})
}
return {
add :add1,
delete :remove,
items :listItems
}
}
const myCollection=collection()
myCollection.add("Chandan")
myCollection.add("Batman")
myCollection.add("Superman")
myCollection.add("Noob")
myCollection.delete("Noob")
myCollection.items()
function store(fn){
const mp= new Map()
return function(...args){
const key=String(args)
if(mp.has(key)){
return mp.get(key)
}
const result=fn(...args)
mp.set(key,result)
return result
}
}
function square(num1){
return num1*num1
}
const str=store(square)
console.log(str(4))
console.log(str(4))
console.log(str(5))
console.log(str(5))
function factorial(num1){
if(num1==1) return 1;
return num1*factorial(num1-1)
}
const str2=store(factorial)
console.log(str2(5))
console.log(str2(5))
console.log(str2(4))
console.log(str2(4))