Make Coding Challenge: "Return a Sorted Array Without Changing the Original Array" accept .slice() to pass tests.
See original GitHub issueFeature Request: Add array.slice()
as valid method to clone/copy array and as an alternative to [].concat(array)
Link to challenge: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/return-a-sorted-array-without-changing-the-original-array/
I often use array.slice() to return an array copy, as this is what MDN recommends. When I was working on this challenge I could not get it to pass, even though my solution was returning the correct answer. The challenge ONLY accepts answers that use the format [].concat(arr). The wording in the challenge is clear, but experience from earlier challenges hava been lenient as long as you provide a valid solution.
Other reasons:
-
When googling for array copying the top result from MDN recommends slice.()
-
MDN’s Array page recommends using arr.slice() for array copying https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
-
Personal opinion,
arr.slice(0)
is more readable than[].concat(arr)
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
You have to also view the test text as part of the instructions for the challenges. In this case, one of the the challenge test expects you to use the
concat
method. It even mentions theconcat
method in the Description section of the challenge. Always read the test text to make sure you understand what is being asked of you.@TimAndreJacobsen and @icatat After thinking a bit more about your points, I agree this challenge could be updated to allow any valid method for copying the array (
arr.slice()
,[],concat(arr)
,[...arr]
,Array.from(arr)
) and other possible ways not mentioned. While looking at the challenge tests, I also realized someone could still technically hard-code the final return value, so I created a PR #35801 to address your concerns and attempt to not allow hard coding of the final array returned.