logo
logo

Custom Exception Constructor

Beginner

Custom Exception Constructor

Create two functions,
NegativeNumberException
and
calculateSquareRoot
, that will be used to calculate the square root of a number, but throw an exception if the number is negative.
  1. NegativeNumberException(value)
    should take a single argument, the negative number passed to
    calculateSquareRoot
    , and should define an object that has two properties:
    value
    and
    message
    . The
    value
    property should be set to the negative number passed in as an argument, and the
    message
    property should be set to the string 'Number must be positive'. Additionally, the
    toString
    method of the object should return a string that concatenates the
    value
    and
    message
    properties, separated by a space.
  2. calculateSquareRoot(number)
    should take a single argument, a number. If the number is negative, it should throw a
    NegativeNumberException
    object with the value of the argument. If the number is positive, it should return the square root of the number using the
    Math.sqrt
    function.

Console
Submit
Solution
00:00