* fix: move "Explore Differences Between..." to basic JS, update seed and tests * fix: resequence "Declare String Variables" * fix: move "Declare a Read-Only Variable..." to basic JS, update seed and tests * fix: revert changes to non-English "Explore Differences Between..." test text * fix: revert test strings, solutions, and seeds for non-English challenges * fix: update "Declare String Variables" description * fix: sync quotation marks in description and seed * fix: modify note in "Declare a Read-Only..." challenge * fix: update operator and compound assignment challenges * fix: update string challenges * fix: update array and array method challenges * fix: update function and scope challenges, resequence slightly * fix: "Word Blanks" solution * fix: add spacing to seed * fix: concatenating += challenge spacing * fix: appending variables to strings spacing * fix: find the length of a string spacing * fix: removed instances of removedFromMyArray = 0 * fix: switch challenges * fix: function argument and param spacing * fix: update counting cards, object challenges, and record collection * fix: finish rest of Basic JS section * fix: introducing else statements solution * fix: update spacing and wording * fix: update wording for const challenge * fix: update functional programming challenges * fix: intermediate algorithms and cert challenges * fix: revert some spacing and remove comments for fp challenge solutions * feat: add notes with links to moved let and const challenges in first two es6 challenges * fix: update es6 intro text * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: concatenating strings with plus operator seed * fix: add comments back to Declare a Read-Only Variable... seed * feat: add es6 to basic javascript redirect tests for let and const challenges * fix: revert "Concatenating Strings with Plus Operator" seed * fix: move test file to cypress/integration/learn/redirects, separate redirect tests Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
92 lines
2.3 KiB
Markdown
92 lines
2.3 KiB
Markdown
---
|
|
id: 587d7b87367417b2b2512b42
|
|
title: Mutate an Array Declared with const
|
|
challengeType: 1
|
|
forumTopicId: 301206
|
|
dashedName: mutate-an-array-declared-with-const
|
|
---
|
|
|
|
# --description--
|
|
|
|
If you are unfamiliar with `const`, check out [this challenge](/learn/javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword).
|
|
|
|
The `const` declaration has many use cases in modern JavaScript.
|
|
|
|
Some developers prefer to assign all their variables using `const` by default, unless they know they will need to reassign the value. Only in that case, they use `let`.
|
|
|
|
However, it is important to understand that objects (including arrays and functions) assigned to a variable using `const` are still mutable. Using the `const` declaration only prevents reassignment of the variable identifier.
|
|
|
|
```js
|
|
const s = [5, 6, 7];
|
|
s = [1, 2, 3];
|
|
s[2] = 45;
|
|
console.log(s);
|
|
```
|
|
|
|
`s = [1, 2, 3]` will result in an error. The `console.log` will display the value `[5, 6, 45]`.
|
|
|
|
As you can see, you can mutate the object `[5, 6, 7]` itself and the variable `s` will still point to the altered array `[5, 6, 45]`. Like all arrays, the array elements in `s` are mutable, but because `const` was used, you cannot use the variable identifier `s` to point to a different array using the assignment operator.
|
|
|
|
# --instructions--
|
|
|
|
An array is declared as `const s = [5, 7, 2]`. Change the array to `[2, 5, 7]` using various element assignments.
|
|
|
|
# --hints--
|
|
|
|
You should not replace `const` keyword.
|
|
|
|
```js
|
|
(getUserInput) => assert(getUserInput('index').match(/const/g));
|
|
```
|
|
|
|
`s` should be a constant variable (by using `const`).
|
|
|
|
```js
|
|
(getUserInput) => assert(getUserInput('index').match(/const\s+s/g));
|
|
```
|
|
|
|
You should not change the original array declaration.
|
|
|
|
```js
|
|
(getUserInput) =>
|
|
assert(
|
|
getUserInput('index').match(
|
|
/const\s+s\s*=\s*\[\s*5\s*,\s*7\s*,\s*2\s*\]\s*;?/g
|
|
)
|
|
);
|
|
```
|
|
|
|
`s` should be equal to `[2, 5, 7]`.
|
|
|
|
```js
|
|
assert.deepEqual(s, [2, 5, 7]);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
const s = [5, 7, 2];
|
|
function editInPlace() {
|
|
// Only change code below this line
|
|
|
|
// Using s = [2, 5, 7] would be invalid
|
|
|
|
// Only change code above this line
|
|
}
|
|
editInPlace();
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
const s = [5, 7, 2];
|
|
function editInPlace() {
|
|
s[0] = 2;
|
|
s[1] = 5;
|
|
s[2] = 7;
|
|
}
|
|
editInPlace();
|
|
```
|