* fix: clarify test description * fix: improve wording regarding rest parameters syntax Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: improve with better sounding wording Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
		
			
				
	
	
		
			79 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
---
 | 
						|
id: 587d7b88367417b2b2512b47
 | 
						|
title: Use the Rest Parameter with Function Parameters
 | 
						|
challengeType: 1
 | 
						|
forumTopicId: 301221
 | 
						|
dashedName: use-the-rest-parameter-with-function-parameters
 | 
						|
---
 | 
						|
 | 
						|
# --description--
 | 
						|
 | 
						|
In order to help us create more flexible functions, ES6 introduces the <dfn>rest parameter</dfn> for function parameters. With the rest parameter, you can create functions that take a variable number of arguments. These arguments are stored in an array that can be accessed later from inside the function.
 | 
						|
 | 
						|
Check out this code:
 | 
						|
 | 
						|
```js
 | 
						|
function howMany(...args) {
 | 
						|
  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.
 | 
						|
```
 | 
						|
 | 
						|
The rest parameter eliminates the need to check the `args` array and allows us to apply `map()`, `filter()` and `reduce()` on the parameters array.
 | 
						|
 | 
						|
# --instructions--
 | 
						|
 | 
						|
Modify the function `sum` using the rest parameter in such a way that the function `sum` is able to take any number of arguments and return their sum.
 | 
						|
 | 
						|
# --hints--
 | 
						|
 | 
						|
The result of `sum(0,1,2)` should be 3
 | 
						|
 | 
						|
```js
 | 
						|
assert(sum(0, 1, 2) === 3);
 | 
						|
```
 | 
						|
 | 
						|
The result of `sum(1,2,3,4)` should be 10
 | 
						|
 | 
						|
```js
 | 
						|
assert(sum(1, 2, 3, 4) === 10);
 | 
						|
```
 | 
						|
 | 
						|
The result of `sum(5)` should be 5
 | 
						|
 | 
						|
```js
 | 
						|
assert(sum(5) === 5);
 | 
						|
```
 | 
						|
 | 
						|
The result of `sum()` should be 0
 | 
						|
 | 
						|
```js
 | 
						|
assert(sum() === 0);
 | 
						|
```
 | 
						|
 | 
						|
`sum` should be an arrow function which uses the rest parameter syntax (`...`) on the `args` parameter.
 | 
						|
 | 
						|
```js
 | 
						|
assert(__helpers.removeWhiteSpace(code).match(/sum=\(\.\.\.args\)=>/));
 | 
						|
```
 | 
						|
 | 
						|
# --seed--
 | 
						|
 | 
						|
## --seed-contents--
 | 
						|
 | 
						|
```js
 | 
						|
const sum = (x, y, z) => {
 | 
						|
  const args = [x, y, z];
 | 
						|
  return args.reduce((a, b) => a + b, 0);
 | 
						|
}
 | 
						|
```
 | 
						|
 | 
						|
# --solutions--
 | 
						|
 | 
						|
```js
 | 
						|
const sum = (...args) => {
 | 
						|
  return args.reduce((a, b) => a + b, 0);
 | 
						|
}
 | 
						|
```
 |