-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable-scope.js
More file actions
750 lines (528 loc) · 17.7 KB
/
Copy pathvariable-scope.js
File metadata and controls
750 lines (528 loc) · 17.7 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
// Defining Variables:-
/*let and var is used to declare variables. Const is used to declare a Constant:-- let name="Jack";
varage="35";
const earn=1000; */
//---------------------------------------------------
//Naming Convention:--
/*ALLOWED:-
-let userName(camelCase)
-let cric24(only letters & digits)
-let $special(starting with $)
-let _internalValue(starting with _allowed) */
/*NOT ALLOWED:--
-let user_name(Bad Practice)
-let 21Players(starting with Numbers)
-let user-b(special characters not allowed)
-let let */
//------------------------------------------------
//Variable Declarationn and Variable Initialization
/*
var msg;
msg="Hello JavaScript!";// assigned a string value
console.log(msg);// access a variable
//the following declares and assign a numeric value
var num=100;
var hundred=num;// assigned a variable to varible
console.log(hundred);
console.log(num);
*/
/*
let msg;
msg="Hello JavaScript!";// assigned a string value
console.log(msg);// access a variable
//the following declares and assign a numeric value
let num=100;
let hundred=num;// assigned a variable to varible
console.log(hundred);
console.log(num);
*/
/*
const msg; //Only Declared and not initialized(Error)
msg="Hello JavaScript!";// assigned a string value(it will not take this value)
console.log(msg);// Cannot access a variable
//the following declares and assign a numeric value
const num=100;
const hundred=num;// assigned a variable to varible
console.log(hundred);
console.log(num);
*/
//---------------------------------------
//ARITHEMATIC OPERATORS
//1. + [Add Two Numbers](right to left)
let a=5, b=5;
console.log(a + b);
//2. - [Subtract two Numbers](right to left)
let a=5, b=5;
console.log(a - b);
//3. * [Multiply two numbers](left to right)
let a=5, b=5;
console.log(a * b);
//4. / [divide two numbers](left to right)
let a=5, b=5;
console.log(a / b);
//5. % [divide two numbers and gives remainder](left to right)
let a=5, b=5;
console.log(a % b);
//6. ** [exponentiation] (2 ** 3 =8)
let a=5, b=5;
console.log(a ** b);
//7. = [Assign Value]
//8. += [perform addition and re-Assign Value]
let a=5, b=6;
console.log(a += b);
//9. -= [Perform Substraction and re-Assign value]
let a=5, b=5;
console.log(a -= b);
//10 ++ [Increment and re-assign]
//POST-INCREMENT
let a=5;
console.log(a++); //5, Because it will increment the value by One but Evaluates/Assign the Value Before Increment
//PRE-INCREMENT
let b=5;
console.log(++b); //6, Because it will increment the value by one and Evaluates/Assign the NEW VAlUE.
//11. -- [Decrement And re-Assign]
let a =5;
console.log(a--); //5, Because it will decrement the value by One but Evaluates/Assign the Value Before decrement
console.log(a--); //4
let a =5;
console.log(--a); //4, Because it will decrement the value by One and Evaluates/Assign the Value after decrement
console.log(--a); //3
//--------------------------------------------------
//Number and String
let saving = 300;
console.log(`Hello CK. You have net worth of $${saving}`);
console.log('Hello CK. You have net worth of $'+saving+'')
console.log("Hello CK. You have net worth of $"+saving+"")
//ESCAPE Characters
// --\n = new Line
// --\t = Horizontal Tabulator
// --\' = Single Quote
// --\" = Double Quote
//------------------------------------------------
//ARRAY
let arr= [1,2,3,4,5];
let age=30;
let arr1= ["ck",2,arr, ["a","b"],age];
console.log(arr1[0]);
console.log(arr1[3][0]);
console.log(arr1[2][4]);
//------------------------------------------------------------
//OBJECTS
let obj= {name:"CK",
age:30,
nationality:"Indian",};
console.log(obj);
console.log(obj.name);
console.log(obj["nationality"]);
//----------------------------------------------------------
/*
let name='2';
console.log(parseInt(name)+2); //4, Adds the Numbers-- parseInt converts string into Numbers
console.log(name+2); //22, it takes '2' as a string
*/
//-------------------------------------------------------
//Function
// function add(a, b) {
// let result = a + b;
// console.log(result)
// }
// add(3, 4);
//-------------------------------------------------------------
// function add(a, b) {
// const results = a + b;
// return results;
// }
//--------------------------------------------------------
//typeof Operator
function getMessage() {
console.log("this is test message");
return 5;
}
let arr= [1,2,3,4];
let obj= {name:"CK",
age:30,
nationality:"Indian",};
let age= 30;
let myName="CK";
console.log("Type of UNDEFINED :"+ typeofundefined); //it will not work, it is not in BRACKET..
console.log("Type of NULL :"+ typeof(null));
console.log("Type of NaN :"+ typeof(NaN));
console.log("Type of Function :"+ typeof(getName));
console.log("Type of Array :"+ typeof(arr));
console.log("Type of Object :"+ typeof(obj));
console.log("Type of Number :"+ typeof(age));
console.log("Type of String :"+ typeof(name));
console.log("Type of Boolean :"+ typeof(myName=="CK"));
console.log(typeof(nationality));
console.log("Type of Function: " + typeof(Function));
arr =[12,44,55,3,552,]
console.log(arr[1]);
console.log(typeof(arr));
console.log(typeof(Null)); //object
console.log(typeof(arr)); //object
console.log(typeof ("John")) // Returns "string"
console.log(typeof (3.14)) // Returns "number"
console.log(typeof (NaN)) // Returns "number"
console.log(typeof (falsse)) // Returns "boolean"
console.log(typeof ([1,2,3,4])) // Returns "object"
console.log(typeof ({name:'John', age:34})) // Returns "object"
console.log(typeof (new Date())) // Returns "object"
console.log(typeof (function () {})) // Returns "function"
console.log(typeof (myCar)) // Returns "undefined" *
console.log(typeof (null)) // Returns "object"
console.log(typeof(undefined));
//--------------------------------------------------------------
//UNDEFINED:- Default value of uninitialized variables
//NULL:- never assumed by default
//NaN:- result of invalid calculations
//------------------------------------------------------
//Array
const arr1= [1,2,3];
console.log(arr1);
const arr = new Array(5);
console.log(arr) //5 Empty items
const arr1 = Array(5);
console.log(arr1); //5 empty items
const arr2 = Array.of(1, 2);
console.log(yetMoreNumbers); //error
console.log(arr2); //[1, 2]
/CONVERT STRING INTO ARRAY
const arr3 = Array.from("hello");
console.log(arr3); //Split [h, e, l, l, o]
//----------------------------------------------------------
// //Array Operations(push, pop, shift, unshift)
const hobbies= ["Sports","Cooking"];
console.log(hobbies.push("Reading")); // add element at the end of the array
console.log(hobbies);
console.log(hobbies.unshift("Coding")); // add at the begining of the array
console.log(hobbies);
const poppedValue=hobbies.pop(); // Remove from the end of the array
console.log(poppedValue); //and returns the remvoed element
let removedEle = hobbies.shift(); // Remove fromthe beginning of the arrray
console.log(removedEle);
console.log(hobbies);
//----------------------------------------------------------
//SLICE
const arr = [23, 553, 46, 677, 57];
console.log(arr.slice(1, 3));
//-------------------------------------------------------------
//SPLICE() METHOD
const hobbies= ["Sports","Cooking"];
console.log(hobbies.splice(0,0,1,"one"));
console.log(hobbies);
let val=hobbies.splice(0,0,"singing");
console.log(val);
console.log(hobbies);
const removedElements=hobbies.splice(-2,2,"running");
console.log(hobbies);
console.log(removedElements);
console.log(hobbies);
//-----------------------------------------------------
//ARRAY CONCAT
const arr1 =[12, 33, 44, 555];
let newArr = arr1.concat([23424, 23525, 235542]);
console.log(newArr.push(34));
console.log(newArr);
//------------------------------------------------------------
//Indexof and Lastindex
const testResults= [1, 5.3, 1.5, 10.99, -5, 1.5, 10];
console.log(testResults.indexOf(1.5));
console.log(testResults.indexOf(1.5,3));
console.log(testResults.lastIndexOf(1.5));
console.log(testResults.lastIndexOf(1.5, -1));
console.log(testResults.lastIndexOf(22));
const personData= [{name:"CK"}, {name:"Prabhat"}];
console.log(personData.indexOf({name:"CK"}));
console.log(personData.lastIndexOf({name:"prabhat"}));
//----------------------------------------------------------------
/* Q2. write a program to print the below pattern of stars.
*
**
***
****
***** */
//A2:-
/*
for (var i=1; i<=5; i++){
console.log("*".repeat(i));
}
//--------------------------------------------------------------------------------
/* Q3. write a program to print the pyramid of stars. */
//A3.
function pyramid(n){
for(let i=1; i<=n; i++){
let str =" ".repeat(n-i);
let str2 ="*".repeat(i*2 -1)
console.log(str + str2 + str);
}
}
pyramid(5);
//----------------------------------------
//REVERSE PYRAMID
let n = 5;
// External loop
for (let i = 0; i < n; i++) {
// printing spaces
for (let j = 0; j < i; j++) {
process.stdout.write(' ')
}
// printing star
for (let k = 0; k < 2 * (n-i) - 1; k++) {
process.stdout.write('*')
}
console.log();
}
//-------------------------------------------------------------
//when we have array of objects we use the find() and findIndexOf() to take out the element NOT INDEX..
//Find()
//findIndex()
const personData = [{name:"VK"}, {name:"NN"}];
console.log(personData.indexOf({name:"VK"}));
const myName = personData.find((person, idx, persons) => {
console.log(idx, "idx");
return person.name === "VK"
});
console.log(myName, personData)
const myIndex =personData.findIndex((person, idx, persons) => {
console.log(idx, "index");
return person.name === "NN"
})
console.log(myIndex);
//-----------------------------------------------------------
//ARRAY REVERSE
//BEST SHORT METHOD
var arr = [1, 2, 3, 4, 5, 6, 7];
console.log (arr.reverse());
//------------------------------------------------------------
//ARRAY SORT[BUBBLE SORT]
let numbers = [0, 123 , 223, 53, 130, 20, 30 ];
numbers.sort( function( a , b){
if(a > b) return 1; //always use positive value for ascending sort here, use negative value to sort descending
if(a < b) return -1; //always use negative value for ascending here, use positive value to sort desscending
return 0;
});
console.log(numbers)
//---------------------------------------------------------
//SORT-REVERSE together
const prices= [5.99,10.99,3.99,6.59];
const names= ["ck","Prabhat","Yogesh","Haresh"];
const info= [{name:"CK"}, {name:"Prabhat"}, {name:"Haresh"}];
let sortedValue=prices.sort((a,b) => {if(a>b) {
return 1; //always use positive value here, use negative value to sort reverse
} else if(a===b) {
return0;
} else {
return -1; //always use negative value here, use positive value to sort reverse
}});
console.log(prices);
console.log(sortedValue);
console.log(sortedValue.reverse());
//--------------------------------------------------
//Filter()
const prices = [22, 555, 56, 567]
let filterValue = prices.filter((price, idx, prices) => {
return price > 500;
});
console.log(prices);
console.log(filterValue);
//---------------------------------------------------------
//Includes()
const test = [12,3313,441,4141,41414,1241455,]
console.log(test.indexOf(441,1));
console.log(test.includes(12));
console.log(test.includes(54545));
console.log(testResults.indexOf(11) !== -1); //FALSE
//--------------------------------------------------------------
//forof-- for Array and forin--- for Objects
//forEach() is like forof
const arr =[2.22, 13, 665,-433, 677.3]
for(let value of arr)
console.log(value);
for(let o in arr) {
console.log(o)
console.log(arr[o])
}
//forEach
const articlePrice = [2133, 13331 ,1233, 3646, 57678];
const shipping = 1500;
const totalPrices = [];
for(let value of articlePrice) {
totalPrices.push(value + shipping);
}
console.log(value);
console.log(totalPrices);
articlePrice.forEach((value, idx, articlePrice) => {
const artvalue = { index: idx, totalPrice: value + shipping}
totalPrices.push(artvalue);
});
console.log(totalPrices);
//--------------------------------------------------------
//MAP
const articlePrices= [10.99,5.99,3.99,6.59];
const tax=1.2;
const totalPrices=articlePrices.map((value, idx, articlePrices) => {
const artvalue= {index:idx, totalPrice: value + tax };
return artvalue;
});
console.log(totalPrices);
//--------------------------------------------------------
//ARRAY DESTRUCTING
const userDetails= ["Max","Schwarz","Mr",30];
// const firstName = userName[0];
// const lastName = nameData[1];
const[firstName,lastName, ...otherInformation]=nameData;
console.log(firstName);
console.log(lastName);
console.log(otherInformation);
//----------------------------------------------------
//ARRAY Reduce
const prices= [10.99,5.99,3.99,6.59];
const sum=prices.reduce((total, curValue, currentIndex, arr) => {
return total + curValue;
},0);
console.log(prices);
console.log(sum);
//-------------------------------------------------------
//SPLIT() //CONVERT STRING TO ARRAY
const text = "Welcome to JavaScript Tutorial - ItsJavaScript";
// split the text into words using space as delimiter
const substr = text.split(" ");
console.log(substr);
//split the text into SINGLE LETTERS
const sub = text.split("");
console.log(sub);
//------------------------------------------------------------
//JOIN() //CONVERT ARRAY TO STRING
const name = ["V", "C"];
let joinedname = name.join("");
console.log(joinedname)
const myText = "It is a long established fact that a reader will"
const newText = myText.split(" ").join("")
console.log(newText);
//----------------------------------------------------
//SPREAD OPERATOR
// const numbers = [11,231,14,4155,];
// const altNumbers = [...numbers];
// // console.log(altNumbers);
// console.log(altNumbers.push(6767, 98));
// // console.log(numbers);
// console.log(altNumbers);
//
const prices= [10.99,5.99,3.99,6.59];
const copiedPrices= [...prices];
prices.push(12.99);
console.log(prices,copiedPrices["0"]);
//------------------------------------------------------
//REVERSE with For Loop
// var arr = [1, 2, 3, 4];
// for (let i = 0; i < Math.floor(arr.length / 2); i++) {
// [arr[i], arr[arr.length - 1 - i]] = [arr[arr.length - 1 - i], arr[i]];
// }
// console.log(arr);
//------------------------------------------------------------
//Reverse with function reverse
function reverse(array) {
var output = [];
while (array.length) {
output.push(array.pop());
}
return output;
}
console.log(reverse([1, 2, 3, 4, 5, 6, 7]));
//----------------------------------------------------------
//*Reverse a STRINg with Function with "" i.e COMMA
let string = "vinay";
console.log(string.split("").reverse().join(""));
//*Reverse a String with Function witout ""
let str = "vinay";
console.log(str.split("").reverse().join());
//------------------------------------------------------------
//Reverse String
// program to reverse a string
function reverseString(str) {
// // empty string
let newString = "";
for (let i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
}
// take input from the user
const string = ('Hello World');
const result = reverseString(string);
console.log(result);
//-----------------------------------------------------
//Reverse String with Function
// program to reverse a string
function reverseString(str) {
// return a new array of strings
const arrayStrings = str.split("");
// reverse the new created array elements
const reverseArray = arrayStrings.reverse();
// join all elements of the array into a string
const joinArray = reverseArray.join("");
// return the reversed string
return joinArray;
}
// take input from the user
const string = ('Hello');
const result = reverseString(string);
console.log(result);
//-------------------------------------------------
//IF-ELSE IF -ELSE
let age = 158;
if(age < 18) {
console.log("not adult");
} else if (age > 48) {
console.log("you are old");
} else {
console.log("you are adult")
};
//-------------------------------------------------------------------------------
//Logical Operators
/*
*AND(&&)
*OR(||)
*NOT(!)
*/
//AND OPERATOR(&&)
-used for boolean operations on two values.
-returs true if and only if its first and second Operand are true.
let x =10, y =10;
if (x===10&&y===10){
console.log("true");
}else {
console.log("false");
//OR (||)
performs boolean expressions on two relational values.
-returs true if one or both operands are true.*/
let x=10, y=10;
if (x===10||y===10) {
console.log("true");
} else {
console.log("false")
};
//NOT (!)
-Unary operator
-it is placed before the single operand.*/
if X =True;
use ! i.e
!x =False
//---------------------------------------------------------
//EQUALITY
let obj1 = {name:"CK"};
let obj2 = {name:"CK"};
console.log(obj1==obj2);
console.log(obj1===obj2);
let obj3 = obj2;
console.log(obj2===obj3);
console.log(obj2==obj3);
let arr1 = ["sports","cooking"];
let arr2 = ["sports","cooking1"];
let arr3 = arr2;
console.log(arr1==arr2);
console.log(arr1===arr2);
console.log(arr2===arr3);
//--------------------------------------------------
//TERNARY OPERATOR
2 > 3 ? console.log("true value") : console.log("false");