-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrest_operator.html
66 lines (58 loc) · 1.55 KB
/
rest_operator.html
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
<!DOCTYPE html>
<html>
<head>
<title>Rest operator uses</title>
</head>
<body>
<h2>Rest operator uses</h2>
<script>
/*
Rest parameter example 1
*/
function getFruitNames(firstName , ...lastNames) {
console.log(firstName);
console.log(lastNames);
}
getFruitNames('Mango', 'Banana', 'Apple', 'Pipneapple');
/*
Rest parameter example 2
*/
// passing arguments are needed to convert array
function multiply() {
let arg = Array.from(arguments);
let result = arg.reduce(function(accumulator, currentvalue) {
return accumulator * currentvalue;
});
console.log(result);
}
multiply(1, 2);
multiply(1, 2, 5);
// using rest operator is no needed to convert array
function multiply_rest(...arg) {
let result = arg.reduce(function(accumulator, currentvalue) {
return accumulator * currentvalue;
});
console.log(result);
}
multiply_rest(1, 2);
multiply_rest(1, 2, 5);
/*
Rest parameter example 3
*/
function sum(a, b) {
return a + b;
}
console.log(sum(1, 2)); // results 3
console.log(sum(1, 2, 5)); // results 3 becuase only taking first two parameters, other parameters are being ignored
function sum_rest(...input){
let sum = 0;
for(let i of input){
sum+=i;
}
return sum;
}
console.log(sum_rest(1, 2)); // results 3
console.log(sum_rest(1, 2, 5)); // results 8
</script>
</body>
</html>