-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.js
More file actions
1171 lines (901 loc) · 26.7 KB
/
Copy pathfunction.js
File metadata and controls
1171 lines (901 loc) · 26.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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------
//function Declaration
function add(a, b) { //formal
let result = a + b;
console.log(result);
}
//Calling function
add(3,4) //actual parameter
function add(a, b) {
const results = a + b;
alert("thanks");
return results;
// alert('Thanks'); //any statement after return will not execute
};
//---------------------------------------------------------------
//Direct and Indirect Calling
function mahol(){
console.log("ho");
console.log("gaya");
}
mahol();*/ //call function
//You directly execute a function: someFunction()
//You schedule a function for future execution: el.addEventListener ('click', someFunction)
//------------------------------------------------------------
// //function as a Statement
function add(a, b) {
const results = a + b;
console.log(results);
// return results;
}
add(3, 4); //without RETURN
// function add(a, b) {
const results = a + b;
// console.log(results);
return results;
}
console.log(add(3, 4)); //With Return
//Function as a Expression (ANONYMOUS FUNCTION)
let add = function (a, b) {
let results = a + b;
return (results);
}
console.log(add(3, 4));
//---------------------------------------------------------------
//FUNCTION WITH METHODS-- FUNCTION INSIDE AN OBJECT IS CALLED METHOD and STORED WITH KEY
let person = {
name: "VC", //property
//this is Method
greet: function () { //greet is KEY
console.log("hello V");
}
};
//-----------------------------------------------------------
//ANONYMOUS FUNCTION --function withoout NAME, stored in a variable
let add = function (a, b) {
const results = a + b;
return results;
}
console.log(add(4, 4));
//ANONYMOUS FUNCTION WITH NAME -- ERROR(addNum not Defined)
let add = function addNum (a, b) {
const results = a + b;
return results;
}
add(4,4);
addNum(5,5);
//----------------------------------------------------------------------------------------
/* arrow notation(()=>{}) */
//always anonymous
//three way syntax:-
/*
1.you can omit the function keyword.
2.if a single parameter then you can omit the parentheses.
3.if single statement then you can omit the curly Braces.
*/
//EXAMPLE
const add = (a, b) => {
const result = a + b;
return result;
}
console.log(add(6,6));
//------------------------------------------------------------
//DEFAULT VALUE TO PARAMETER
function multiply(a, b=1) {
a= a || 3;
return a * b;
}
console.log(multiply(5,2)); //value of a is 5 & b is 2, because ACTUAL parameter will REplace FORMAl value 1.
//expected output: 10
function multiply(a, b=1) {
a = a||3;
return a * b;
}
console.log(multiply()); //value of a is 3 and b is 1
// expected output: 3
//----------------------------------------------------------
//Default Arguments in functions
let add = function (a, b) {
console.log(arguments);
const results = arguments[0] + arguments[1]; //Here, [0] and [1] are index to access from ACTUAL parameter
return results;
}
console.log(add(3, 4));
//DEFAULT ARGUMENTS with ARROW
////Default Arguments are not Available in ARROW FUNCTIONS
let showArgs= () => {
console.log(...arguments);
};
showArgs(1,2,3); //ERROR-NOT DEFINED
//----------------------------------------------------
//IIFE-Immediately Invoked Function Expression
//NAMED and ANONYMOUS function Supports IIFE
(function () {
console.log("this is IIFE ANONYMOUS");
})();
// // arrow function
(() => {
console.log("this is IIFE arrow function");
})();
//--------------------------------------------------
//REST OPERATOR(PASSED AS PARAMETER TO FUNCTION)
//rest operator will combile the rest of the values and gives an Array of values and we can use index to access each value from array
//It Converts the values in ARRAY
const sumUp = (a, b, ...numbers) => {
let sum=0;
for(const num of numbers) {
console.log(num);
}
}
console.log(sumUp(1,5,10, -3,6,10)); //10, -3 6, 10
// const sumUp = (a,b, ...numbers) => {
// let sum=0;
// for(const num of numbers) {
// sum += num;
// }
// return sum;
// }
// console.log(sumUp(1,5,10, -3,6,10)); //23
// console.log(sumUp(1,5,10, -3,6,10,25,88)); //136
//--------------------------------------------------------
//CALLBACK FUNCTIONS -is a function passed as an argument to another function
function main(firstname, lastname) {
console.log("hello, my name is " + firstname + " " + lastname);
}
//callback function
function displayname () {
main("vinay", "choudhary");
}
displayname();
//-----------------------------------------------------------
//CLOSURE
/* closure */
//function inside another function.
//use return keyword.
//values are retained in closure and not get lost
//use of lexical scope
//Example:-
var sum=function(a){
console.log("hello "+a);
var c=4;
return function(b){
return a+b+c;
}
}
//create a variable to store return
var store=sum(2); //jo function sum function ne diya h wo store kiya h STORE m h
console.log(store(5)); //CLOSURE PROCESS:- when we called var store=sum(2) it reaches to var sum function and we get hello 2, now we call console.log(store(5)) which goes inside the sum function to the RETURN function and give b=5 and it also access the A=2 and C=4 in return of the RETURN function(because in CLOSURE VALUES ARE RETAINED EVEN WHEN WE CALL TWO FUNCTIONS, FUNCTION INSIDE ANOTHER FUNCTION)
//-----------------------------------------------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------
//OBJECTS(add, modify, delete)
let person = {
name:"CK",
"Last Name":"Pradhan",
age:30,
hobbies:["Sports","Cooking"],
greet:function () {
alert("Hi there!");
},
1.5:"One point five",
12: "cc"
};
//Accessing property of Object
console.log(person);
console.log(person.name);
console.log(person.hobbies);
person.hobbies.push("SS");
console.log(person.hobbies);
console.log(person.hobbies[0]);
console.log(person["Last Name"]);
person["Last Name"] = "M";
console.log(person["Last Name"]);
console.log(person[1.5]); //number dont need ""
console.log(person[12]);
//Accessing a property which is not in the object
console.log(person.isAdmin); //UNDEFINED
//Adding or editing property//
person.isAdmin = true; //Nothing
person[12] = "VV";
console.log(person[12]);
person["isAdmin"] = false; //fasle
console.log(person.isAdmin);
person.mark = 23; //ADD NEW PROPERTY
console.log(person.mark);
person.age = 31;
console.log(person);
person.age = undefined;
console.log(person);
person.age = null;
console.log(person.age); //null
console.log(person);
//Deleting property
delete person.age;
console.log(person);
//---------------------------------------------------------------
//for-in LOOPS
let person= {
name:"CK",
"Last Name":"Pradhan",
age:30,
hobbies:["Sports","Cooking"],
greet:function() {
alert("Hi there!");
},
1.5:"One point five",
};
for(let p in person) {
console.log(person[p]);
}
for(p in person) {
console.log(`${p} => ${person[p]}`);
}
//----------------------------------------------------
//Accessing PROPERTIES
// const arr4 =[
// {name:"ak", no:34},[
// //declare two objects
// {name:"sos", subjects:"js"},
// {
// name:"os", subjects:"literals"
// }
// ]
// ];
// console.log(arr4[1][0].name);
// console.log(arr4[0].no);
// console.log(arr4[1][1].subjects);
//-------------------------------------------------------------
//OBJECT SPREAD OPERATOR(SPILTS THE ARRAY)
//spread operator breaks/divides the combined values
//ARRAY/OBJECTS values SPREAD
// let person = {
// name:"VV",
// age:30,
// hobbies:["Sports","Cooking"],
// address:{
// country:"India",
// state:"Gujarat",
// city:"Ahmedabad",
// },
// greet:function() {
// alert("Hi there!");
// },
// };
// let person2= { ...person};
// console.log(person2==person); //false
// person.profession="Programmer";
// person.hobbies.push("Singing");
// person.address.city="Surat";
// person.address.pin= 310;
// // console.log("person 1",person1); //person1 not defined
// console.log("person 2",person2);
// console.log(person);
// // Restrict affect on inner layer
// let person3 = {
// ...person,
// hobbies: [...person.hobbies],
// address: { ...person.address },
// };
// //Alternate way of spread operator
// let person2 = Object.assign({}, person);
//-------------------------------------------------------------
//OBJECT DESTRUCTURRING --store a key in multiple
// let obj= {
// name:"CK",
// age:30,
// address:"Ahmedabad",
// hobbies:["sports","singing"],
// };
// // obj.hobbies.push("SS");
// // console.log(obj);
// let address= obj.address;
// let {
// hobbies,
// name} =obj;
// console.log(hobbies); //print Hobbies
//---------------------------------------------------------
// //THIS KEYWORD
//refer to current object, with arrow does not refer to current
// let obj= {
// name:"CK",
// age:30,
// address:"Ahmedabad",
// getName:function() {
// return`My name is ${this.name}`;
// },
// };
// console.log(obj.getName());
//-------------------------------------------------------------------
//PROTOTYPE CONSTRUCTOR -- uses THIS KeyWORD to Declare Variables
// 1. A constructor is a function that creates an instance of a class which is typically called an “object”.
// 2. In JavaScript, a constructor gets
// called when you declare an object using the new keyword.
// 3. The purpose of a constructor is to create an object and
// set values if there are any object properties present.
// function Person() {
// this.age=30;
// this.name="CK";
// this.greet= function() {
// console.log("Hi, I am "+ this.name + " and I am " + this.age +" years old.");
// };
// }
// Person.prototype= {
// printAge() {
// console.log(this.age);
// },
// };
////CREATE A OBJECT WITH NEW KEYWORD
// let p = new Person();
// p.greet(); //Y
// p.printAge(); //Y
//-------------------------------------------------------------------
//CALL, APPLY, BIND
//Call invokes the function and allows you to pass in arguments one by one.(Function BORROWING)
//Apply invokes the function and allows you to pass in arguments as an array.
//Bind returns a new function, allowing you to pass in a this array and any number of arguments.
//CALL
// let userDetails = {
// name: "V",
// age: 44,
// printDetails: function () {
// console.log(this.name);
// console.log(this.age);
// }
// }
// userDetails.printDetails();
// let userDetails2 = {
// name: "VX",
// age: 443,
// }
// userDetails.printDetails.call(userDetails2);
// let obj= {
// fname:"ChandraKant",
// lanme:"Pradhan",
// age:30,
// address:"Ahmedabad",
// formatName:function() {
// //console.log(this);
// console.log(`My name is ${this.fname} ${this.lanme}`);
// },
// formatName: () => {
// console.log(this);
// console.log(`My name is ${this.fname} ${this.lanme}`);
// }
// };
//Access the method directly referring to the Object
// obj.formatName(); //Y
// //Access the methods indirectly and later on
// let formatName = obj.formatName;
// let { formatName } = obj;
// formatName = formatName.bind(obj);
// formatName();//
//Access the methods indirectly and immediately
// formatName.call(obj, 2, 4, 5);
// takes parameter with comma separated
// formatName.apply(obj, [4, 2.6]); // take parameteras an array
//------------------------------------------------------------
//GETTERS and GETTERS
// getters => access properties
// setters => change or mutate the
// let obj = {
// set name(val) {
// if(val.trim() ==="") {
// this.myName="CK";
// return;
// }
// this.myName=val;
// },
// getname() {
// return this.myName;
// },
// age:30,
// address: "Ahmedabad",
// formatName:function() {
// //console.log(this);
// console.log(`My name is${this.fname}${this.lanme}`);
// },
// };
// obj.name="Chandrakant";
// console.log(obj.name);
//-------------------------------------------------------------------
//---------------CLASS---------------------------------------
//-------------------------------------------------------------------
//----------------------------------------------------------
// //NOT(!) OPERATOR
// let userInput= "";
// let isUserInput = userInput ? true : false; //FALSE
// let isUserInput = !userInput ? true : false; //TRUE
// let isUserInput= !!userInput; //FASLE
// console.log(isUserInput);
//----IF-ELSE
// let userInput = "",
// let isUserInput = !userInput ? true : false; //let is disallowed in lexical bound
// if(isUserInput) {
// console.log("this is true");
// }else{
// console.log("this is flase"); //error
// }
//---OR(||) Operator---------------------
// || used to assign default value
// let realUserName = "Chandrakant"; //channdrakanta
// let realUserName = "";
// let userName = realUserName || "CK";
// console.log(userName); //CK
//------AND (&&) OPERATOR------------
// let isLoggedin = true;
// let shoppingCart = isLoggedin && ["Books, Fruits"]; //books, fruits
// let shoppingCart = isLoggedin && null; //null
// console.log(shoppingCart)
//------SWITCH-CASE-------------------
//one VALUE
// let val="CK";
// switch(val) {
// case "CK":
// console.log("This is CK");
// console.log("CK is going to Market");
// break;
// case "Prabhat":
// console.log("This is Prabhat");
// console.log("Prabhat is going to School");
// break;
// default:
// console.log("No one is there");
// console.log("No Activity is performed");
// }
///--------FOR LOOP------------
//executes loop over CONDITION
// for( let i = 0; i<3; i++) {
// console.log("------------"); //-----------
// } for( let i = 10; i>0; i--) {
// console.log(i) //10,9,8,7.....
// };
//-----FOR OF---------------------------------
//Executes Over each ELEMENT of ARRAY[]
// let arr = ["CK","Prabhat","Pankaj"];
// let str = "This is string";
// for(const value of arr) {
// console.log(arr.indexOf(value), ":", value);
// console.log(); //value with INDEX
// }
//-----FOR IN---------------------------------
//executes over each KEY in OBJECT
// // Single loop
// let obj = {
// name: "CK",
// age:30,
// hobby:"music, cricket",
// };
// for(const o in obj) {
// console.log(`${o}=>${obj[o]}`);
// }
// //loop inside loop
// let arr = [{
// name:"CK",
// age: 30,
// hobby: "Playing cricket",
// },
// {
// name:"Prabhat",
// age:22,
// hobby:"Singing",
// },
// {
// name:"Pankaj",
// age:28,
// hobby:"Dncing",
// },
// ];
// let i = 0;
// for(const element of arr) {
// console.log(`#${i}`);
// for(const key in element) {
// console.log(`${key}=>${element[key]}`);
// }
// i++;
// }
//----BREAk and CONTINUE-----------------------------------
//Break statement Will BREAK the EXECUTION at the POINT and TERMINATE THE LOOP.
//CONTINUE will BREAK the EXECUTION at ONLY That Point and Will CONTINUE after that.
// for( let i = 0; i<10;i++) {
// if(i==5) {
// // break; //0,1,2,3,4,BREAK
// continue; //0,1,2,3,4,BREAK,6,7,8,9
// }
// console.log(i);
// }
// for ( let i = 0; i < 5; i++) {
// if (i == 2) {
// continue;
// }
// console.log(i);
// for (let i = 0; i < 10; i++) {
// if (i == 5) {
// break;
// }
// console.log(`i:${i}`);
// }
// }
//---------------------------------------------------------
//------CLASSES AS CONSTEUCTOR----------------------------
//-------------------------------------------------------
// class person {
// //we dont use keyword function for PARAMETER in CLASS INSTEAD we use CONSTRUCTOR KEYWORD for PASSING PARAMETERs
// constructor(name, age) {
// this.name = name;
// this.age = age;
// }
// greet() {
// console.log(`Hello, i'm ${this.name} and my age is ${this.age}`);
// console.log("For " +this.name+ " Honesty is the Best Policy");
// }
// };
// //CALL CONSTRUCTOR WITH THE KEY NEWKEYWORD
// let constructPerson = new person("Jacky", 28);
// constructPerson.greet();
// class Person2 {
// constructor(age,name) {
// // this.age =age||30;
// // this.name =name||"CK"; //Retur PRABHAT AND 25
// // this.age =age && 30;
// // this.name =name && "CK"; //Return CK and 30
// }
// greet() {
// console.log(`Hi, I am ${this.name} and I am ${this.age} years old`);
// }
// }
// let classObj= new Person2(25,"Prabhat");
// classObj.greet();
//-----------------------------------------------------------
//STATIC METHOD
// class Person2 {
// constructor(age,name) {
// this.age =age||30;
// this.name=name||"CK";
// }
// greet() {
// console.log(`Hi, I am ${this.name} and I am ${this.age} years old`);}profession="IT Professional";
// //ADD PROPERTY wit STATIC KEYWORD
// static address ="Ahmedabad";
// //ADD METHOD WITH STATIC KEYWORD
// static showAddress() {
// console.log(`I am staying in ${this.address}`);
// }
// };
// let constructPerson = new Person2(28, "Jacky");
// constructPerson.greet();
// console.log(constructPerson.profession);
// Person2.showAddress();
// // constructPerson.showAddress(); //ERROR
// // console.log(constructPerson.showAddress()); //ERROR
//---------------------------------------------------------
//EnCApsulation
// class person {
// constructor(name,id) {
// this.name =name;
// this.id =id;
// }
// add_Address(add) {
// this.address =add;
// this.getDetails = function() {
// console.log(`Name is ${this.name}, Address is: ${this.address}`);
// };
// this.getDetails();
// }
// }
// let person1 = new person("CK",21);
// person1.add_Address("Ahmedabad");
//--------------------------------------------------------
// //INHERITANCE
// class person {
// constructor(name) {
// this.name=name;
// }
// //method to return the string
// displayName1() {
// return`Name of person: ${this.name}`;
// }
// }
// class student extends person {
// constructor(name,id) {
// //super keyword for calling above class constructor
// super(name);
// this.id=id;
// }
// displayName(params) {
// console.log(params);
// console.log(` ${super.displayName1()},
// Student ID: ${this.id}` );
// }
// }
// let student1 = new student("Mukul", 22);
// student1.displayName();
//-------------------------------
//-----INHERITANCE INSTANCEOF OPERATOR--------------
// class person {
// constructor(name) {
// this.name =name;
// }
// //method to return the stringd is
// displayName() {
// return`Name of person: ${this.name}`;
// }
// }
// class student extends person{
// #id="";
// constructor(name,id) {
// //super keyword for calling above class constructor
// super(name);
// this.#id= id;
// }
// displayName() {
// console.log(` ${super.displayName()},
// StudentID: ${this.#id}`);
// }
// }
// let student1 = new student("Mukul",22);
// student1.displayName();
// // console.log(student1.#id);
// //CALL the INSTANCEOF Operator
// console.log(student1 instanceof student);
//-------------------------------------------------------------
//------------------------------------------------------------
/* object */
//object is like a container of properties.
//example
/*
const obj={
name:"string", vale:33
};
*/
/* array VS object */
//array can access using index.
//object can access using string or symbol
//arrays are ordered
//objects are not ordered
//example(to access all the properties)
/*
const obj={
f1:"hello",
f2:"everyone",
f3:"welcome",
f4:"back to",
f5:"daily tution"
};
//using control flow statement
for (let f in obj){
console.log(obj[f]);
}
/* HOW TO ACCESS OBJECTS In DIFFERENT WAYS */
// let object1 = {
// a: 'somestring',
// b: 42,
// };
// console.log(object1.a); //something
// console.log(object1.b); //42
// console.log(object1["a"]); //something
// /* use this to get values */
//for(const o in object1){
//console.log(object1[o])
//}; //prints somestring and 42
/* use this to get the keys */
//for(const o in object1){
//console.log(o);
//} //prints a and b
/* use this to get the key value pairs */
//for (const [key, value] of Object.entries(object1)) {
//console.log(`${key}: ${value}`);
//} //prints k:v
/* use this to get the array of key value pairs */
//console.log(Object.entries(object1)); //prints [...]
//forkeys:-
//console.log(Object.keys(object1));
//for values:-
//console.log(Object.values(object1));
/* use this to get the keys */
//for(const key of Object.keys(object1)){
//console.log(`${key}`)
//}; //prints a and b
/* use this to get the values */
//for(const value of Object.values(object1)){
//console.log(`${value}`)
//}; prints somestring and 42
//-------------------------------------------------------------
//------------------------------------------------------------
/* arrow notation(()=>{}) */
//always anonymous
//three way syntax:-
/*
1.you can omit the function keyword.
2.if a single parameter then you can omit the parentheses.
3.if single statement then you can omit the curly Braces.
*/
//EXAMPLE
// const add = (a, b) => {
// const result = a + b;
// return result;
// }
// console.log(add(6,6));
//Shorter parameter syntax, if exactly one parameter is received:
// const log= (message) => {
// console.log(message);
//Empty parameter parentheses if NO arguments are received:
// const greet= () => {
// console.log('Hi there!');
// };
//Shorter function body, if exactly one expressionis used:
// const add= (a,b)=>a+b;
//-----------------------------------------------------------------------------------------
//----------------------------------------------------------------
/* 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();
}
/* function */ //parameters are optional
//functions are a block of codes designed to perform a particular task.
//syntax
/*function funname(arg, ..., arg){
statements;
}*/
//example
/*
function mahol(){
console.log("ho");
console.log("gaya");
}
mahol();*/ //call function
//example with return
/*