Course: JavaScript

Progress (0%)

  1. Foundation

  2. Numbers

  3. Strings

  4. Conditions

  5. Loops

  6. Arrays

  7. Functions

  8. Objects

  9. Exceptions

  10. HTML DOM

  11. CSSOM

  12. Events

  13. Drag and Drop

  14. opt Touch Events

  15. Misc

  16. Project: Analog Clock

Exercise: Required Arguments

Exercise 39 Easy

Prerequisites for the exercise

  1. JavaScript Exceptions — Basics
  2. All previous chapters

Objective

Create a function to add two values together whereby being provided with the values (as arguments) and with the appropriate ones is required, or otherwise an error gets thrown.

Description

If a function in a statically-typed language, such as C, C++ and Java, is set up with a given parameter and then if an argument isn't provided to the function for that parameter in the function's invocation, an error is raised.

This is why we generally say that statically-typed languages have a strict type system in place. For a function, if it is set to accept an argument, then it must be provided one at invocation.

JavaScript, however, isn't a statically-typed language and has very loosely-defined type rules.

Talking about functions in JavaScript, if a function is set up with a number of parameters, then it's perfectly alright to invoke the function without providing any arguments to it at all, as demonstrated below:

function sum(a, b) {
   return a + b;
}

sum(); // This is perfectly alright!

JavaScript won't throw any kind of errors. To make the parameters required, we ought to enforce this ourselves.

In this exercise, you have to define a function sum() that returns the sum of two given values, provided as arguments, when coerced into numbers.

It should work as follows:

  • If the function is called without two arguments, an appropriate error must be thrown.
  • If the arguments can't be coerced into a number, an appropriate error must be thrown. For instance, true converts to 1 and is therefore a valid value. In contrast, 'Hello' converts to NaN and is therefore an invalid value.
View Solution

New file

Inside the directory you created for this course on JavaScript, create a new folder called Exercise-39-Required-Arguments and put the .html solution files for this exercise within it.

Solution

To get done with the first requirement of the function as given above, we'll lay out an if conditional to check if either of the arguments a and b is undefined. If this is the case, then a TypeError ought to be thrown with the message that an argument wasn't supplied.

This is accomplished below:

function sum(a, b) {
   if (a === undefined || b === undefined) {
      throw new TypeError('sum() must be called with two arguments.');
   }
}

Next up, we'll convert both a and b into a number using the Number() function and then check both of the resulting values. If the result of either conversion is NaN, then we'll throw a TypeError saying that the given arguments weren't convertible to a meaningful number.

This is accomplish below:

function sum(a, b) {
   if (a === undefined || b === undefined) {
      throw new TypeError('sum() must be called with two arguments.');
   }

   a = Number(a);
   b = Number(b);
   if (isNaN(a) || isNaN(b)) {
      throw new TypeError("The arguments provided to sum() can't be coerced into a meaningful number.");
   }
}

If neither of the conditions above gets met, then we must return the sum of the numbers, as done below:

function sum(a, b) {
   if (a === undefined || b === undefined) {
      throw new TypeError('sum() must be called with two arguments.');
   }

   a = Number(a);
   b = Number(b);
   if (isNaN(a) || isNaN(b)) {
      throw new TypeError("The arguments provided to sum() can't be coerced into a meaningful number.");
   }

   return a + b;
}

This completes the sum() function.

It's now time to test it.

In the following console snippet, we call sum() without any arguments and thus get the required-argument error thrown:

sum()
Uncaught TypeError: sum() must be called with two arguments. at sum (<anonymous>:3:13) at <anonymous>:1:1

Perfect!

Moving on, in the following snippet, we call sum() with the arguments 10 and {}. Since the latter converts into the number NaN, we get an error thrown:

sum(10, {})
Uncaught TypeError: The arguments provided to sum() can't be coerced into a meaningful number. at sum (<anonymous>:9:13) at <anonymous>:1:1

Perfect still!

Lastly, in the following snippet, we call sum() with two such arguments that get converted into meaningful numbers, and thus get the return value without running into any errors:

sum(10, '5')
15

Flawless.

And this completes this exercise.