* fix: consolidate comments Co-authored-by: Parth Parth <34807532+thecodingaviator@users.noreply.github.com>
2.6 KiB
2.6 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7b87367417b2b2512b42 | Mutate an Array Declared with const | 1 | 301206 |
Description
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.
"use strict";
const s = [5, 6, 7];
s = [1, 2, 3]; // throws error, trying to assign a const
s[2] = 45; // works just as it would with an array declared with var or let
console.log(s); // returns [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
const s = [5, 7, 2]
. Change the array to [2, 5, 7]
using various element assignment.
Tests
tests:
- text: You should not replace <code>const</code> keyword.
testString: getUserInput => assert(getUserInput('index').match(/const/g));
- text: <code>s</code> should be a constant variable (by using <code>const</code>).
testString: getUserInput => assert(getUserInput('index').match(/const\s+s/g));
- text: You should not change the original array declaration.
testString: getUserInput => assert(getUserInput('index').match(/const\s+s\s*=\s*\[\s*5\s*,\s*7\s*,\s*2\s*\]\s*;?/g));
- text: <code>s</code> should be equal to <code>[2, 5, 7]</code>.
testString: assert.deepEqual(s, [2, 5, 7]);
Challenge Seed
const s = [5, 7, 2];
function editInPlace() {
'use strict';
// Only change code below this line
// Using s = [2, 5, 7] would be invalid
// Only change code above this line
}
editInPlace();
Solution
const s = [5, 7, 2];
function editInPlace() {
'use strict';
s[0] = 2;
s[1] = 5;
s[2] = 7;
}
editInPlace();