[Easy] LeetCode JS 30 - 2703. Return Length of Arguments Passed
March 5, 2024
☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee
LeetCode 30 Days of JavaScript
This question is from LeetCode's 30 Days of JavaScript Challenge
2703. Return Length of Arguments PassedQuestion Prompt
Write a function argumentsLength
that returns the count of arguments passed to it.
// Example 1
Input: args = [5]
Output: 1
Explanation:
argumentsLength(5); // 1
One value was passed to the function so it should return 1.
// Example 2:
Input: args = [{}, null, "3"]
Output: 3
Explanation:
argumentsLength({}, null, "3"); // 3
Three values were passed to the function so it should return 3.
Solutions
To solve this problem, we need to first understand the three dots (...
) in the function definition of argumentsLength(...args)
. This is called a "rest parameter." This feature allows a function to accept an indefinite number of arguments and gathers them into an array named args
.
Inside the argumentsLength
function, we simply return args.length
. The length
property of an array provides the number of elements in the array. In this case, args
holds all the arguments passed to the function.
function argumentsLength(...args) {
return args.length;
}