Freecodecamp: [beta] Challenge mutate an array declared with const - Need Clearer instructions

Created on 12 May 2017  路  7Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge mutate-an-array-declared-with-const has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36.

Please describe how to reproduce this issue, and include links to screenshots if possible.

This problem can be solved by changing the value of const s ......In the description it tell that code below and code above this line ....



const s = [ 2, 5, 7 ];
// change code below this line


// change code above this line
// Test your code
console.log(s);

help wanted

All 7 comments

This challenges seed and the instructions feel misleading and a bit ambiguous.
/cc @freeCodeCamp/moderators

I think the description is decent, but I think the bigger issue are the challenge tests themselves.

In terms of the description, here's a potential rewrite:

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[2] = 45; // works just as it would with an array declared with var or let

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 assigned to s is mutable, but because const was used, you cannot use the variable identifier s to point to an entirely different array using the assignment operator.

Note
To make an object immutable, you can use Object.freeze().

An array is declared as const s = [5, 7, 2]. Change the array to [2, 5, 7] using various element assignments.

Note
Don't forget to add "use strict"; to the top of your code.

The challenge tests obviously need work, as two of them don't even have tests to check.

So here are some code that might help

assert(code.match(/const/g), 'message: Do not replace `const` keyword');
assert(code.match(/const\\s+s/g), 'message: <code>s</code> is declared with <code>const</code>');
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: <code>s</code> should be equal to <code>[2, 5, 7]</code>.');
assert(!Object.isFrozen(s), 'message: <code>s</code> should not be frozen.');

The note on using Object.freeze() isn't really relevant to this challenge. Should we remove this? I'm not sure how important this is.

@erictleung I agree that we should add more tests as you suggest. Would you be interested in creating a PR?

I agree that the "should not be frozen" message is confusing. I had to look it up myself: "Object.freeze() prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed."

Perhaps we can figure out a simpler way to word this?

@erictleung I agree, Object.freeze() should not be included in this challenge. However, it would be a great follow up to this challenge - immutable objects and arrays are really useful knowledge.

If this challenge is created, should we perhaps open a separate issue for it?

@QuincyLarson How about something like this:

Object.freeze() prevents any further changes to an object. This includes adding, removing or changing the values of properties and is useful to store constant data

@erictleung have you had a chance to update this challenge?

@QuincyLarson I haven't had time to work on it because I've been busy traveling for school lately. I'll try to work on it during this upcoming week and comment here if I'm unable to make time for it so that others can take this up.

@erictleung OK - no worries. Thanks for keeping me posted. This is a beta challenge so it's OK if it takes another week or two for you to get to it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robwelan picture robwelan  路  3Comments

MichaelLeeHobbs picture MichaelLeeHobbs  路  3Comments

DaphnisM picture DaphnisM  路  3Comments

ROWn1ne picture ROWn1ne  路  3Comments

kokushozero picture kokushozero  路  3Comments