In the grand tapestry of existence, numbers dance and intertwine, spinning a rhythm of reason that orchestrates the universe. You are tasked with crafting an exquisite instrument, a Calculator
, to join this cosmic ballet.
Certainly, here are the instructions for this exercise:
-
Setup: Define a class named
Calculator
. The constructor of the class should have two properties:PI
which is equal toMath.PI
E
which is equal toMath.E
-
Methods: Implement the following methods:
-
ratio(x, y, width)
: This method should take three parameters -x
,y
andwidth
, and returns the height based on the ratio x:y -
percentage(x, y)
: This method should take two parameters -x
andy
. Calculate the percentage ofx
iny
and return it as a string with a '%' sign. Ify
is zero, return the error message 'Error: Divisor cannot be zero'. -
add(x, y)
: This method should take two parameters -x
andy
. Return the sum ofx
andy
. -
subtract(x, y)
: This method should take two parameters -x
andy
. Return the difference of it (y - x). -
multiply(x, y)
: This method should take two parameters -x
andy
. Return the product ofx
andy
. -
divide(x, y)
: This method should take two parameters -x
andy
. Return the quotient ofx
divided byy
. Ify
is zero, return the error message 'Error: Divisor cannot be zero'. -
remainder(x, y)
: This method should take two parameters -x
andy
. Return the remainder ofx
divided byy
. Ify
is zero, return the error message 'Error: Divisor cannot be zero'. -
elevate(x, y)
: This method should take two parameters -x
andy
. Return the power ofx
raised toy
. -
sqrt(x)
: This method should take one parameter -x
. Return the square root ofx
. Ifx
is a negative number, return the error message 'Error: Cannot calculate the square root of a negative number'.
-
- Remember to handle edge cases, such as division by zero and square root of a negative number.
- Ensure that all strings returned are in the exact format as specified in the instructions.
- Read more on: Euler's Number
console.log(calc.ratio(5, 7, 300)); // 420
console.log(calc.percentage(3, 12)); // 25%
console.log(calc.add(5, 7)); // 12
console.log(calc.subtract(5, 7)); // 2
console.log(calc.multiply(5, 7)); // 35
console.log(calc.divide(35, 7)); // 5
console.log(calc.modulate(7, 5)); // 2
console.log(calc.elevate(5, 3)); // 125
console.log(calc.sqrt(25)); // 5