-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.js
More file actions
51 lines (42 loc) · 1.57 KB
/
Copy pathcalculator.js
File metadata and controls
51 lines (42 loc) · 1.57 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
/** 7$ Tasks
Create an object named calculator with methods add, subtract, and multily#
Imlement the calculate method in the calculator object, which takes an oeration ('add', 'subtract', or
'multily') and two numbers#
Use call to erform an addition oeration using the calculate method#
Use aly to erform a multilication oeration using the calculate method with arguments as an array#
Create another object named discountCalculator with a discount ercentage roerty and a method
alyDiscount#
Use bind to create a new function calculateDiscount that is bound to the discountCalculator object and
can be reused. */
const calculator = {
add(a, b) {
return a + b;
},
subtract(a, b) {
return a - b;
},
multiply(a, b) {
return a * b;
},
calculate(operation, a, b) {
return this[operation](a, b);
}
};
const addResult = calculator.calculate.call(calculator, 'add', 10, 5);
console.log('Addition Result:', addResult);
const multiplyResult = calculator.calculate.apply(calculator, ['multiply', 10, 5]);
console.log('Multiplication Result:', multiplyResult);
const discountCalculator = {
discountPercentage: 10,
applyDiscount(amount) {
return amount - (amount * this.discountPercentage / 100);
}
};
const calculateDiscount = discountCalculator.applyDiscount.bind(discountCalculator);
const discountedPrice = calculateDiscount(100);
console.log('Discounted Price:', discountedPrice);
/** output
Addition Result: 15
Multiplication Result: 50
Discounted Price: 90
*/