-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
80 lines (75 loc) · 1.6 KB
/
script.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
//normal function
var arr = [12,13,14,15,16];
var result =[];
function odd(arr){
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
return result;
}
console.log(odd(arr));
//anonyomus function
var arr = [12,13,14,15,17,19,21]
var result =[];
var odd = function(arr){
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
return result;
}
console.log(odd(arr));
//IIFE:
( function odd(arr){
var result =[];
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
console.log (result);
}
)([12,13,14,15,17,19,21])
//arrow functions:
var odd = (arr)=>{
var result =[];
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
return result;
}
console.log(odd([12,13,14,15,17,19,21]))
//function without parameter
function greet(){
console.log("welcome");
}
// greet();
//template literals
var a = 10;
var b= 20;
console.log(`the sum of a and b = ${a+b}`);
//spread operator : converts a string into character array
//synatx: ...
//example1
var arr=["guvi"]
console.log(...arr);
//example 2:
var arr1=["bob","join","sofia"]
var arr2 = ["rupan" , "guvi"]
var arr3 = [...arr2 , ...arr1];
console.log(arr3)
//rest parameter : it is used inside the function
//to consider remaining values
function foo(...rest){
var sum =0;
for(var i=0;i<rest.length;i++){
sum = sum+rest[i];
}
return sum;
}
console.log(foo(12,13,14,15,16,17))