60 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
		
		
			
		
	
	
			60 lines
		
	
	
		
			3.4 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
|   | --- | ||
|  | title: Mutate an Array Declared with const | ||
|  | --- | ||
|  |  | ||
|  | 
 | ||
|  |  Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code  | ||
|  | 
 | ||
|  | ### Problem Explanation:
 | ||
|  | 
 | ||
|  | Reassign the values of the `const` variable `s` using various element assignment. | ||
|  | 
 | ||
|  | ##  Hint: 1
 | ||
|  | 
 | ||
|  | *   You can change array values on `const` like you can with `var` or `let`. | ||
|  | 
 | ||
|  | > _try to solve the problem now_
 | ||
|  | 
 | ||
|  | ##  Hint: 1
 | ||
|  | 
 | ||
|  | *   To access array value use array[index] | ||
|  | 
 | ||
|  | > _try to solve the problem now_
 | ||
|  | 
 | ||
|  | ## Spoiler Alert!
 | ||
|  | 
 | ||
|  |  | ||
|  | 
 | ||
|  | **Solution ahead!** | ||
|  | 
 | ||
|  | ##  Basic Code Solution:
 | ||
|  | ```javascript | ||
|  |     const s = [5, 7, 2]; | ||
|  |     function editInPlace() { | ||
|  |       "use strict"; | ||
|  |       s[0] = 2; | ||
|  |       s[1] = 5; | ||
|  |       s[2] = 7; | ||
|  |     } | ||
|  |     editInPlace(); | ||
|  | ``` | ||
|  |  <a href='https://codepen.io/dylantyates/pen/djoVjW' target='_blank' rel='nofollow'>Run Code</a> | ||
|  | 
 | ||
|  | # Code Explanation:
 | ||
|  | 
 | ||
|  | Trying to reassign a read-only `const` variable will throw an error, but by using various element assignment you can access and change the value of an array just like you would with `let` or `var`. | ||
|  | 
 | ||
|  | #### Relevant Links
 | ||
|  | 
 | ||
|  | *   <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const' target='_blank' rel='nofollow'>const</a> | ||
|  | *   <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array' target='_blank' rel='nofollow'>Array</a> | ||
|  | 
 | ||
|  | ##  NOTES FOR CONTRIBUTIONS:
 | ||
|  | 
 | ||
|  | *    **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution. | ||
|  | *   Add an explanation of your solution. | ||
|  | *   Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**.  | ||
|  | *   Please add your username only if you have added any **relevant main contents**. ( **_DO NOT_** _remove any existing usernames_) | ||
|  | 
 | ||
|  | > See  <a href='http://forum.freecodecamp.com/t/algorithm-article-template/14272' target='_blank' rel='nofollow'>**`Wiki Challenge Solution Template`**</a> for reference.
 |