Skip to curriculum
Jump to the editor

7.1. Count a function's declared parameters and the arguments it received

Easy
Topics
10–15 min

JavaScript never complains about the number of arguments. Call a function of three parameters with one, and the other two are undefined; call it with ten, and seven are quietly ignored. Two numbers tell you what happened: how many parameters the function declares, and how many arguments a call received. Libraries that curry functions, check calls or route events by how many arguments a handler takes read both.

Write three functions in arity.js.

Signature

JAVASCRIPT
function countArguments()          // returns a number
function countParameters(fn)       // returns a number
function requireArguments(fn)      // returns a function
  • countArguments(...): returns how many arguments the call received.
  • countParameters(fn): returns how many arguments fn expects, as the language counts them: the parameters before the first one with a default value, with a rest parameter never counted.
  • requireArguments(fn): returns a function that throws a TypeError when it receives fewer arguments than fn expects, and otherwise calls fn with its this and every argument and returns the result.

Requirements

  1. countArguments counts every argument passed, including an explicit undefined and the holes of a spread array, however many there are.
  2. countParameters counts the parameters before the first default value, ignores a rest parameter, counts a destructured parameter as one, and works for arrow functions, classes and bound functions.
  3. The function from requireArguments throws a TypeError without calling fn when it gets too few arguments; an explicit undefined counts as an argument.
  4. With enough arguments (more is fine), it calls fn with its own this and every argument, and returns fn's result.
  5. It expects the same number of arguments as fn: its own length matches.

Examples

JAVASCRIPT
countArguments();               // 0
countArguments(1, undefined);   // 2
countArguments(...[1, , 3]);    // 3
JAVASCRIPT
countParameters((a, b, c) => {});        // 3
countParameters((a, b = 2, c) => {});    // 1
countParameters((a, ...rest) => {});     // 1
countParameters(({ x, y }, [z]) => {});  // 2
JAVASCRIPT
const area = requireArguments((width, height) => width * height);
area(3, 4);          // 12
area(3);             // throws TypeError
area(3, undefined);  // NaN: two arguments were passed
area.length;         // 2

Environment

  • Plain JavaScript in a browser page; the tests call the three globals from arity.js.
  • fn is always a function.

Hints

  1. Arrow functions have no arguments object of their own. What else collects every argument?
  2. Every function already knows how many arguments it expects. Which property says so, and is it writable?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…