Count the Values Passed to a Function

Quick Overview

Count actual arguments in a JavaScript function call, treating nested arrays, objects, null and falsy values as individual arguments.

Count the Values Passed to a Function

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Implement a JavaScript function named `argumentsLength` that returns how many arguments were supplied in a call. The function accepts a variable number of arguments. Count each supplied value once, regardless of its type or contents. An array passed as one argument counts as one argument; its elements are not counted separately. With no arguments, return `0`. ### Invocation Format A test fixture may describe a call using a valid JSON array named `inputs`. Its elements become the separate arguments in `argumentsLength(...inputs)`. The fixture array itself is not an additional argument. ### Examples **Example 1:** ```javascript argumentsLength(false, 0, "", null); // 4 ``` Four arguments were supplied. Falsy values and `null` each count as one. **Example 2:** ```javascript argumentsLength([1, 2], {active: true}); // 2 ``` The first argument is one array and the second is one object. Their internal contents do not add arguments. ### Constraints - The argument sequence is represented by a valid JSON array of length `0..100`. - Each argument is a JSON value: null, a boolean, a number, a string, an array or an object, with nested JSON values permitted. - Values outside the JSON domain, such as `undefined`, functions and symbols, are not required inputs. ### What a Strong Answer Covers - Count the arguments actually supplied in each call, including zero arguments. - Treat nested arrays and objects as individual argument values. - Preserve the variadic function interface and count falsy values normally. ### Follow-up Questions - How do `argumentsLength([2, 4, 6])` and `argumentsLength(...[2, 4, 6])` differ? - If one argument contains a deeply nested JSON value, should that nesting affect the result or require inspecting its contents?

Overview: Count actual arguments in a JavaScript function call, treating nested arrays, objects, null and falsy values as individual arguments.

Amazon logo
Amazon
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement a JavaScript function named argumentsLength that returns how many arguments were supplied in a call.

The function accepts a variable number of arguments. Count each supplied value once, regardless of its type or contents. An array passed as one argument counts as one argument; its elements are not counted separately. With no arguments, return 0.

Invocation Format

A test fixture may describe a call using a valid JSON array named inputs. Its elements become the separate arguments in argumentsLength(...inputs). The fixture array itself is not an additional argument.

Examples

Example 1:

argumentsLength(false, 0, "", null); // 4

Four arguments were supplied. Falsy values and null each count as one.

Example 2:

argumentsLength([1, 2], {active: true}); // 2

The first argument is one array and the second is one object. Their internal contents do not add arguments.

Constraints

  • The argument sequence is represented by a valid JSON array of length 0..100 .
  • Each argument is a JSON value: null, a boolean, a number, a string, an array or an object, with nested JSON values permitted.
  • Values outside the JSON domain, such as undefined , functions and symbols, are not required inputs.

What a Strong Answer Covers Guidance

  • Count the arguments actually supplied in each call, including zero arguments.
  • Treat nested arrays and objects as individual argument values.
  • Preserve the variadic function interface and count falsy values normally.

Follow-up Questions Guidance

  • How do argumentsLength([2, 4, 6]) and argumentsLength(...[2, 4, 6]) differ?
  • If one argument contains a deeply nested JSON value, should that nesting affect the result or require inspecting its contents?