From 5522c64172336d006d6de4bd1cbce98c68f15397 Mon Sep 17 00:00:00 2001 From: Eric Leung Date: Fri, 11 Aug 2017 13:06:35 -0700 Subject: [PATCH] fix: Clarify mutate array with const challenge (#15745) Instructions for Mutate an Array Declared with const was unclear and included unnecessary learning materials. This commit aims to clarify the instructions for what the challenge requires, and also add in missing challenge tests. Closes #14832 --- .../es6.json | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/challenges/02-javascript-algorithms-and-data-structures/es6.json b/challenges/02-javascript-algorithms-and-data-structures/es6.json index 491d7af995..cda04ccd00 100644 --- a/challenges/02-javascript-algorithms-and-data-structures/es6.json +++ b/challenges/02-javascript-algorithms-and-data-structures/es6.json @@ -138,29 +138,30 @@ "id": "587d7b87367417b2b2512b42", "title": "Mutate an Array Declared with const", "description": [ - "The const declaration has many use-cases in modern JavaScript.", + "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.", - "
\"use strict\";
const s = [5, 6, 7];
s = [1, 2, 3]; // throws error, trying to assign a const
s[7] = 45; // works just as it would with an array declared with var
", - "As you can see, you can mutate the object ([5, 6, 7]) itself and the variable identifier (s) will still point to the altered array. Like all arrays, the array assigned to s is mutable, but because const was used, you cannot use the variable identifier, s, to point to a different array using the assignment operator.", - "To make an object immutable, you can use Object.freeze().", + "
\"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.", "
", - "An array is declared as const s = [5, 7, 2]. Change the array to [2, 5, 7].", + "An array is declared as const s = [5, 7, 2]. Change the array to [2, 5, 7] using various element assignment.", "Note
Don't forget to add \"use strict\"; to the top of your code." ], "challengeSeed": [ - "const s = [ 5, 7, 2 ];", + "const s = [5, 7, 2];", "// change code below this line", + "", "s = [2, 5, 7];", + "", "// change code above this line", "// Test your code", "console.log(s);" ], "tests": [ - "// Test user did not replace const keyword", - "// Test s is const", - "assert.deepEqual(s, [2, 5, 7], 'message: s should be equal to [2, 5, 7].');", - "assert(!Object.isFrozen(s), 'message: s should not be frozen.');" + "assert(code.match(/const/g), 'message: Do not replace const keyword.');", + "assert(code.match(/const\\s+s/g), 'message: s is declared with const.');", + "assert(code.match(/const\\s+s\\s*?=\\s*?\\[\\s*?2\\s*?,\\s*?5\\s*?,\\s*?7\\s*?\\]\\s*?;/g), 'message: Do not change the original array declaration.');", + "assert.deepEqual(s, [2, 5, 7], 'message: s should be equal to [2, 5, 7].');" ], "type": "waypoint", "releasedOn": "Feb 17, 2017",