3.0 KiB
3.0 KiB
title
title |
---|
Mutate an Array Declared with const |
Remember to use
Read-Search-Ask
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 withvar
orlet
.
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:
const s = [5, 7, 2];
function editInPlace() {
"use strict";
s[0] = 2;
s[1] = 5;
s[2] = 7;
}
editInPlace();
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
.