-
Notifications
You must be signed in to change notification settings - Fork 1
/
59_default_parameters.js
executable file
·48 lines (33 loc) · 1.41 KB
/
59_default_parameters.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
// Default parameters:
function interest(principal, rate, years) {
return principal * rate / 100 * years;
}
console.log("interest", interest(10000, 6, 5));
// What if we want a default value for rate and years.
// Method 01: setting default parameters
// Here, we are using default value for rate and years.
function interest1(principal, rate, years) {
rate = rate || 7;
years = years || 5;
return principal * rate / 100 * years;
}
console.log("interest1", interest1(10000));
// Method 02: setting default parameters
// here, we are setting the default value for rate and years.
// Note: If you a setting a default parameters as above then after rate default parameters any other parameters should also give a default parameter. otherwise code will not work.
function interest2(principal, rate = 8, years = 5) {
return principal * rate / 100 * years;
}
console.log("interest2", interest2(10000));
// Method 03:
// result will be NaN. because here, years is not defined while calling the function.
function interest3(principal, rate = 8, years) {
return principal * rate / 100 * years;
}
console.log("interest3", interest3(10000))
// Method 04:
// The solution for this problem will be to: by adding additional parameters rate = undefined and years=5
function interest4(principal, rate = 8, years) {
return principal * rate / 100 * years;
}
console.log("interest4", interest4(10000, undefined, 5))