function howMany(...args) {The rest operator eliminates the need to check the
return "You have passed " + args.length + " arguments.";
}
console.log(howMany(0, 1, 2)); // You have passed 3 arguments
console.log(howMany("string", null, [1, 2, 3], { })); // You have passed 4 arguments.
args
array and allows us to apply map()
, filter()
and reduce()
on the parameters array.
sum
so that it uses the rest operator and it works in the same way with any number of parameters.
sum(0,1,2)
should be 3'
testString: 'assert(sum(0,1,2) === 3, ''The result of sum(0,1,2)
should be 3'');'
- text: 'The result of sum(1,2,3,4)
should be 10'
testString: 'assert(sum(1,2,3,4) === 10, ''The result of sum(1,2,3,4)
should be 10'');'
- text: The result of sum(5)
should be 5
testString: 'assert(sum(5) === 5, ''The result of sum(5)
should be 5'');'
- text: The result of sum()
should be 0
testString: 'assert(sum() === 0, ''The result of sum()
should be 0'');'
- text: The sum
function uses the ...
spread operator on the args
parameter.
testString: 'getUserInput => assert(getUserInput(''index'').match(/function\s+sum\s*\(\s*...args\s*\)\s*{/g), ''The sum
function uses the ...
spread operator on the args
parameter.'');'
```