Hi all,
I’m stuck on the sort Method challenge right now. I tried debugging my code for a while and couldn’t figure out what could possibly be wrong. I checked the solution and it was the same as my code. After scanning my code for typos and not finding any, I just tried deleting my code and copy-pasting the official solution, but even that doesn’t work. What’s going on here?
function alphabeticalOrder(arr) {
// Add your code below this line
return arr.sort(function(a,b) {
return a > b;
});
// Add your code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
Link to challenge: https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method/
Your code passed for me immediately - you may want to try Reset All Code
and refreshing the page, maybe attempting in another browser.
1 Like
Thanks, I downloaded Firefox and it did the trick. This challenge doesn’t work for me in Chrome, Explorer, or Edge for some reason. I was a bit surprised by Chrome here.
You might want to try running Chrome in Incognito Mode or Safe Mode (if that still exists) and see if maybe an extension is causing you issues.
I just gave that a shot, and the challenge still doesn’t work, whether I use Incognito Mode or disable all my extensions. Weird stuff.
1 Like
Hello all,
this challenge still doesn’t work
is there any solution for chrome?
Chrome is fine, it’s the answer you are giving that is wrong.
The callback to sort
should not return true
or false
— that will work on most browsers, but it is supposed to return -1 (or a negative number), 0 or 1 (a positive number).
Note because of the above, the hint is wrong, which I think is where the confusion comes from.
2 Likes
Hi again,
after @DanCouper answer, I checked some documentation and find some good explanation. you’re right and sort does not return -1 , 1 and 0 value. This example solution is totally missing some detail.
Solution;
function alphabeticalOrder(arr) {
return arr.sort(); // sort default order is A-Z
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
Correction for get a hint documentation
function alphabeticalOrder(arr) {
return arr.sort(function(a,b) {
return (a > b) ? 1 : -1; // Compare values and if a > b return 1 otherwise return -1
return 0 // Values equal
});
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
1 Like