Answer for 'Functional Programming: Sort an Array Alphabetically using the sort Method' doesn't work in Chrome
See original GitHub issueDescribe your problem and how to reproduce it:
I was doing the Functional Programming: Sort an Array Alphabetically using the sort Method challenge, in which you’re supposed to sort an array of letters alphabetically. So I tried what I thought was the correct answer. It didn’t work. I looked at the answer and it was exactly the same but with a longer syntax:
return arr.sort((a,b)=> a>b));
I was very confused, I was so confident about that answer! So I started trying different things in codepen and jsfiddle, but that method wasn’t working at all:
// Chrome output
const arr = ["c", "a", "b"];
console.log(arr.sort((a,b)=>a>b)); //["c", "a", "b"]
console.log(arr.sort((a, b) => a.localeCompare(b))); //["a", "b", "c"]
console.log(arr.sort()); //["a", "b", "c"]
Still very confused I opened Firefox and did the same thing. But this time, all methods worked.
// Firefox output
const arr = ["c", "a", "b"];
console.log(arr.sort((a,b)=>a>b)); //["a", "b", "c"]
console.log(arr.sort((a, b) => a.localeCompare(b))); //["a", "b", "c"]
console.log(arr.sort()); //["a", "b", "c"]
It appears that the latest versions of Chrome won’t let you sort arrays alphabetically that way anymore. But I don’t know what would be the right way to tackle that problem.
Probably a good first step is to add a disclaimer to the solution guide explaining the issue.
What do you guys think?
– EDIT: It may be obvious, but I should mention that this is a problem for all the people using later versions of Chrome, because in both the challenge explanation and the solution, the method that is used to sort the array won’t work. So even if you copy and paste the solution, the tests will fail.
Add a Link to the page with the problem: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method
Tell us about your browser and operating system:
- Browser Name: Chrome
- Browser Version: 73.0.3683.86 (Official Build) (64-bit) –
- Browser Name: Firefox
- Browser Version: 67.0b8 (64-bit) –
- Operating System: macOS Mojave 10.14.3
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top GitHub Comments
@guayom Though not reflected in the beta, the challenge was update`d on PR #35756. Also, on PR #35746, I have corrected the suggested solution and improved the hint example. I am just waiting for it to get merged.
@ojeytonwilliams @RandellDawson Oh I see now! That’s perfect! Thanks for your help! With all that said, I’ll close the issue since the changes will eventually get merged 😃