-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathBasic2.js
More file actions
225 lines (136 loc) · 4.24 KB
/
Basic2.js
File metadata and controls
225 lines (136 loc) · 4.24 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// // Hoisting
// // what is the hoisting ?
// console.log(a); // now i can access this
// var a =300;
// // console.log(b) // throw the error
// let b =300;
// console.log(c) // thro err
// const c = 300;
console.log(typeof a) // a = undefined
var a = 300 ;
console.log(typeof typeof c); // string
// falsy value => 0, "", undefined , null, NAN
const checker = "";
const checkernumber = 0;
if(!checker) {
console.log("Checked")
}
if(!checkernumber) {
console.log("Checked1")
}
const checker2 = undefined;
if(!checker2) {
console.log('Checked2')
}
const checker3 = null;
if(!checker3) {
console.log('Checked3')
}
// STRING , NUMBER , BOOLEAN
// Array
const array = [1,2,3, "heyll" , [1,2,3] , true , false , {}];
// length of array is array.length = > 8
// array.length is give you the size of array
for(let i= 0; i<array.length;i++) {
console.log(array[i])
}
console.log(array)
// array.length = 0;
console.log(array)
// this is for getting the index of array
for(let index in array) {
console.log(index)
}
// this is for getting the value of array
for(let value of array) {
console.log(value)
}
//
const newArray = []; // initializing newArray variable with the empty array
console.log("NewArray" ,typeof newArray); // object
// length of array
console.log("Length of array" , newArray.length)
newArray[9] = 90;
console.log("Length of array" , newArray.length) // what value
console.log('New Array' ,newArray );
for(let value of newArray) {
console.log("Value=>" ,value)
}
console.log("This is example1=>" ,typeof newArray[9000])
// whether my variable is array or not ?
let s = "chill"
console.log(typeof s == 'string')
console.log(typeof newArray == 'Array')
console.log("Is array" ,Array.isArray(newArray))
// array Higher order functions forEach
// in the javascript
// normal function
//function declaration
function functionName(a, b) {
console.log('A===>',a , 'B===>' ,b);
}
functionName() // calling or invoking the functions
functionName(23 , 84) // calling or invoking the functions
functionName(23 ) // calling or invoking the functions
functionName(23 , 45, 67, 87) // calling or invoking the functions
// write a function to add two variables
const sumArray = [9,9,9,0,0,0,7,7];
// array can consist number , boolean , symbol , string
// write the functions to calculate the sum of array
// array can consist number , boolean , symbol , string
const sumArray1 = [9,9,9,true , true,0,7,7 , "hello" , 7 , 'chillkaro' , null , undefined];
randomArraySum(sumArray1)
function randomArraySum (array) {
let sum = 0;
for(let value of array) {
if(typeof value == 'number') {
sum = sum+value;
}
}
console.log("Random sum" , sum)
}
// 0 => false
// 1 => true
console.log( "hey true value in number => " , +true)
console.log( "hey true value in number => " , +false)
// converting things in boolean
const s1 = "hey";
const s2 = "";
const arr = []; // this is consider as true
const obj = {};
console.log("s1=>>" ,Boolean(s1))
console.log("arr=>>" ,Boolean(arr))
console.log("obj=>>" ,Boolean(obj))
console.log("s2===>" ,Boolean(s2))
console.log( '(!!s1)' ,!!s1)
const sumArray22 = [9,9,12,'2' ,'3' ,'5' , 'yyy'];
addNumberAndStringNumber(sumArray22)
function addNumberAndStringNumber(array) {
let sum = 0;
for(let value of array) {
if(isNaN(value) || typeof value == 'boolean') {
continue;
}
sum = sum + (+value);
// sum = sum + Number(value);
}
console.log('sumArray22' , sum)
}
const sumArray222 = [9,9,12,'2' ,'3' ,'5' , 'yyy' , true , false , true , '' , undefined , null ];
addNumberAndStringNumber(sumArray222) ;
// console.log("re" , re);
// what will be the result for it ?
console.log(isNaN("")); // false
console.log(isNaN()); // false
console.log( 'typeof Infinity' ,typeof Infinity) //
console.log("Boolean" ,Boolean(Infinity))
console.log("String" , String(Infinity))
console.log("Number" ,Number(Infinity))
function add (s) {
return s;
}
console.log('add()' , add("hey")); //
console.log('add()' , add(12345)); //
console.log('add()' , add(true)); //
console.log('add()' , add(Infinity)); //
console.log('add()' , add([])); //