-
Notifications
You must be signed in to change notification settings - Fork 4
/
30-may.js
117 lines (79 loc) · 1.61 KB
/
30-may.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
let students = [
{
"name": "Kushagra",
"marks": 92
},
{
"name": "Shameer",
"marks": 93
},
{
"name": "Biswabushan",
"marks": 90
}
]
// let maxMarks = Number.MIN_SAFE_INTEGER;
// let index = -1;
// for(let i=0;i<students.length;i++) {
// let student = students[i];
// if(maxMarks < student.marks) {
// maxMarks = student.marks;
// index = i;
// }
// }
// console.log(students[index]);
let array = [1,2,3,4,5,6,7,8,9,10,11]
console.log(Math.max(...array));
console.log([1,2,3,4,5])
// call stack
function a(n) {
if(n === 0) {
return;
}
console.log('a started', n);
a(n-1); // calling function from itself.
console.log('a ended', n)
}
// print no's from 1..10
function printTillN(n) {
if(n === 1) {
return;
}
console.log(n); // pre-recursive.
printTillN(n-1);
//console.log(n); // post-recursive.
}
//printTillN(10);
function factorial(n) {
if(n === 1) {
return 1;
}
return n*factorial(n-1);
}
//console.log(factorial(3))
function fabonacci(n) {
if( n === 0 || n === 1) {
return n;
}
return fabonacci(n-2) + fabonacci(n-1);
}
//console.log(fabonacci(7))
// closure.
// counter.
function Counter() {
let x = 0;
this.increment = function() {
x++;
}
this.print = function() {
console.log(x);
}
}
let counter1 = new Counter();
counter1.increment();
counter1.increment();
counter1.increment();
counter1.print(); // 3
let counter2 = new Counter();
counter2.increment();
counter2.print(); // 1