2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 587d7b8a367417b2b2512b4c
							 
						 
					
						
							
								
									
										
										
										
											2019-05-08 10:30:24 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 1
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301218
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Description
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'description' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In some situations involving array destructuring, we might want to collect the rest of the elements into a separate array.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The result is similar to < code > Array.prototype.slice()< / code > , as shown below:
							 
						 
					
						
							
								
									
										
										
										
											2019-05-17 06:20:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(a, b); // 1, 2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(arr); // [3, 4, 5, 7]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-05-08 10:30:24 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Variables < code > a< / code >  and < code > b< / code >  take the first and second values from the array. After that, because of rest parameter's presence, < code > arr< / code >  gets rest of the values in the form of an array.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The rest element only works correctly as the last variable in the list. As in, you cannot use the rest parameter to catch a subarray that leaves out last element of the original array.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Instructions
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'instructions' >  
						 
					
						
							
								
									
										
										
										
											2019-05-08 10:30:24 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Use destructuring assignment with the rest parameter to perform an effective < code > Array.prototype.slice()< / code >  so that < code > arr< / code >  is a sub-array of the original array < code > source< / code >  with the first two elements omitted.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Tests
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'tests' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```yml
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								tests:
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > arr</ code >  should be < code > [3,4,5,6,7,8,9,10]</ code > 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-27 11:29:53 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(arr.every((v, i) => v === i + 3) & &  arr.length === 8);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: < code > Array.slice()</ code >  should not be used.
							 
						 
					
						
							
								
									
										
										
										
											2019-03-27 11:29:53 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: getUserInput => assert(!getUserInput('index').match(/slice/g));
							 
						 
					
						
							
								
									
										
										
										
											2018-11-22 18:57:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Destructuring on < code > list</ code >  should be used.
							 
						 
					
						
							
								
									
										
										
										
											2019-03-27 11:29:53 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert(code.replace(/\s/g, '').match(/\[(([_$a-z]\w*)?,){1,}\.\.\.arr\]=list/i));
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Challenge Seed
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'challengeSeed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-seed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const source = [1,2,3,4,5,6,7,8,9,10];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function removeFirstTwo(list) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "use strict";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // change code below this line
							 
						 
					
						
							
								
									
										
										
										
											2018-11-22 18:57:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const arr = list; // change this
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  // change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return arr;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const arr = removeFirstTwo(source);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(arr); // should be [3,4,5,6,7,8,9,10]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Solution
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'solution' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-11-22 18:57:16 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const source = [1,2,3,4,5,6,7,8,9,10];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function removeFirstTwo(list) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "use strict";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // change code below this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const [, , ...arr] = list;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return arr;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const arr = removeFirstTwo(source);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 08:24:12 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >