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 the addition of num1 and num2.

  • subtract(): a method which returns the subtraction of num1 from num2

  • multiply(): a method which returns the multiplication of num1 and num2.

  • divide(): a method which returns the division of num2 by num1.

Sample Input

var obj = new Calculator(5,10)
obj.add()
obj.subtract()
obj.multiply()
obj.divide()

Sample Output

15
5
50
2

Get hands-on with 1400+ tech skills courses.