-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnatural.js
More file actions
executable file
·159 lines (109 loc) · 2.95 KB
/
Copy pathnatural.js
File metadata and controls
executable file
·159 lines (109 loc) · 2.95 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// /* return a function that returns all the natural numbers from 1 to n
// sum of 1 to 5 which is : 15 */
// function sumOfNaturalnumber(num){
// let sum = 0 ;
// for (let i =1 ; i<=num; i++){
// sum+=i;
// }
// return sum;
// }
// console.log(sumOfNaturalnumber(5));
// function sum_of_natural(num){
// let sum = 0 ;
// for(let i =1; i<=num; i++){
// sum+=i;
// }
// return sum;
// }
// console.log(sum_of_natural(6));
/* Another way to do the same thing is */
function natural_num_fast(num){
return num*(num+1)/2; //this is the formula , you should've known this, okay !
}
console.log(natural_num_fast(5));
let myArray = ['flash', 'batman', 'myfav: superman'];
let index =0;
while(index<myArray.length){
console.log(myArray[index]);
index++;
}
/**High order array loops */
const arr = [1,2,3,4,5,6];
for(const num of arr){
console.log(num);
}
const greetings = 'Hello World';
let count = 0;
for(const i of greetings){
console.log(i);
count++;
}
console.log(` the count of words are : ${count}`);
//map
const map = new Map();
map.set('BN', 'Bangladesh');
map.set('DE','Germany');
map.set('NP','Nepal');
console.log('Working ? ')
// for(const key in map){
// console.log(key);
// }
for(const [key,value] of map){
console.log(key , '-:-' , value);
}
const myObj = {
'game1': 'Rainbow 6 Siege',
'game2': 'Counter Strike 2',
'game3': 'Overwatch'
}
for(const key in myObj){
console.log(`${key} is my ${myObj[key]}`);
}
const programming_lang = ['js', 'cpp', 'java'];
for( key in programming_lang){
console.log(key ,'=', programming_lang[key]);
}
/**in array By default it starts with 0 , but in the Object it start with anyting that you can enter */
console.log('starting forEach ! ');
programming_lang.forEach((value , index )=>{
console.log(value +':'+index);
})
const country = ['Deutschland', 'Bangladesh', 'France', 'Australia', 'Japan','United States of America','China','Italy','Norway','Thialand'];
country.forEach((value, index , arr)=>{
// console.log(value, index,arr);
})
//very common on fetching data from database :
const database = [
{
languageName : 'JavaScript',
languageDot : '.js'
} ,
{
languageName : 'C++',
languageDot : '.cpp'
} ,
{
languageName : 'Python',
languageDot : '.py'
}
]
database.forEach((items , index, arr)=>{
console.log(items.languageDot);
// console.log(items.languageName);
})
/* filter map and reduce */
//use forEach it never returns anything , but using filter will help you returning the value too !
//if you are using a scope it turns into object then in filter you must use the return keyword to get the value !
const myName = [1,3,4,5,4,2,6,4,8,6,5,3];
const NewNum = myName.filter((nums)=> {
return nums>=4;
} );
console.log(NewNum)
// i can use this way also
const ano_arr = [];
const newVarient = myName.forEach((pnum)=>{
if(pnum>=1){
ano_arr.push(pnum);
}
})
console.log(ano_arr);