Exercise 2: Calculator
In this exercise, you have to implement a calculator which can perform addition, subtraction, multiplication, and division.
We'll cover the following
Problem Statement
A constructor function called Calculator
is given with fields: num1
and num2
. You have to add the following methods to the constructor function:
-
add()
: a method which returns the sum after addition ofnum1
andnum2
. -
subtract()
: a method which returns the subtraction ofnum1
fromnum2
-
multiply()
: a method which returns the multiplication ofnum1
andnum2
. -
divide()
: a method which returns the division ofnum2
bynum1
.
Sample Input
var obj = new Calculator(5,10)
obj.add()
obj.subtract()
obj.multiply()
obj.divide()
Sample Output
15
5
50
2
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy