Change:
<!-- NOTE: If you're reporting a security issue, don't create a GitHub issue. Instead, email We will look into it immediately. --Describe your problem and how to reproduce it: This challenge has an example of sorting like this: ` This example uses a bad compare function. In recent changes to Chrome the following example no longer properly sorts the array: The compare function needs to return a number to be able to sort an array properly. This can be fixed by changing the compare function to return a number: Add a Link to the page with the problem: Tell us about your browser and operating system: Browser Name: Chrome Browser Version: 70 Operating System: Windows 10
The above return statement only returns 1 or -1. To preserve the original order for elements which have the same value, you need to test for the condition where they are equal. For example, to preserve an elements original order when equal, you would write: While in this particular example, you may not be able to see the difference, I will show a different example below, where the difference can be seen. Let's say you have the following array of objects: The 2nd and fifth elements have the same letter, but they are in different positions. Let's say you want to sort the objects in alphabetical order and preserve the original sort order of the elements with the same letter (in this case those with letter 'b'). This means, after the sort, letters should look like: If you just create the following sort function without specifying a return value of 0 when the letters are equal, it would not sort correctly in the new version of Chrome: The above produces: instead of the correct version: To preserve the original sort order for objects with the same letter, you must use the following sort function: I think the example should reflect the better practice of specifying a return value of 0 for elements which are equal in value. This matches the actual requirements of the compare function of returning a value of 0, -1, or 1, so I believe the example should be written like:
I am interested working on this issue
Although the example used in the original text doesn't require ordering I agree adding would be fine. As much as I like ternaries, I'm not in favor of nested ternaries. As a general rule they . If possible I'd keep the text challenge clean and readable. Maybe the good ol' if statements are fine here:
Let's see what says after my last reply, before we assign it to you.
Yeah, even I think it is better to make it in the form of as many people still get confused with nested ternaries.
I would take a different perspective here. For the purposes of the tutorial, I suggest that nested ternaries are the way to go. If people do get confused, this will encourage them to look up their doubt and they will be better versed with ternaries for later. I have opened a PR with the edit implementing nested ternaries.
On second thought, and the fact we already have a nested ternary lesson in basic JS section. I think it's fine to use a nested ternary. Good point as well.
Make sure to reference that it closes this issue # if you have not already done so.
just remember to ask first before you create a PR as there might be other people waiting to do one, like had asked here before if he could work on it? sorry about not getting to do a PR for this issue, there are other issues you might take a look at.
I will take care of this in future PRs. Apologies for my mistake .
Comment on issue
What are these PRs you have referenced?
could be spam.
The sample code on the FCC website is still the old version. I noticed on the challenge's GitHub page the sample code is different than what I saw on the live FCC website. I did this challenge on December 19, 2018. The sample code on the FCC website website is the old version (different from the GitHub version). I thought that the GitHub version of the challenge was reflected on the FCC site. Does this happen at regular intervals, or do updated challenges need to be pushed live to the site? As a side note, the sample code on the GitHub page and the live FCC site doesn't work in Chrome 70+ for reasons addressed above. May I update the sample code to a version that works? In this case, the description would also need updating to explain the code. Should I make the changes as a pull request? Sorry, I'm really new to FCC and Open Source contributing in general. I just want to make sure I do things in the most efficient way. I propose the following (the opposite of the reverse alphabetical sort suggested above): The old version that doesn't work in Chrome 70+: Here's the screenshot: ! Here is the link to the GitHub page:
Not sure if I misunderstood you, by "sample code" do you mean the guide? is a PR for the guide, although it seems to be missing the fix for reverseAlpha ascendingOrder version of sort where the compare function is using subtraction is only (to be) used with numbers, not strings. Which is not explained, nor is the function name very explicit about that fact. On a side note: Personally, I would like to see the use of parentheses as I feel it makes the nested ternary more legible. Maybe also a version using (maybe just in the guide) I would also like to see a link to the MDN docs (and maybe a short explanation of how the method and callback works).
RE: Sort an Array Alphabetically using the sort Method Challenge... Maybe not the cleanest but simply running this code: worked in my case :)
Hi I was not referring to the guide, but instead to the live/production and the GitHub version. (Now that you mentioned it, I checked out the PR you linked and noticed that even in the guide, the first example for an ascending sort does not work in Chrome 70+. I'll comment on that PR as well.) Here are screenshots and tables below summarizing the issue. In short, the sample code on the FCC site for ascending sort is an example of how to sort numbers. It works for numbers but does not work when applied to letters/strings in Chrome 70+. The sample code for the reverse alphabetical sort also doesn't work in Chrome 70+ but does in FireFox. I think it would be best to have sample code that applies to the challenge, and works across major browsers. ! ! PASS PASS PASS PASS Note: there is a typo in the second sample code block of the GitHub version screenshot. The PR you linked addresses that as well. Hope this info helps. Please let me know if anything is unclear.
Hi I like the brackets in the nested ternary. This may be minor, but I think a different arrangement of the nested ternary would be even more intuitive and brackets wouldn't really be necessary. I my suggestions as comments to the code blocks below. They match the code on the MDN page, which states that the ternary operator is right-associative (details below). What do you think? MDN: Conditional (ternary) operator 
The TLDR; The only bug is, Chrome used to accept boolean return values like Firefox still does. After Chrome changed their sort it no longer does. This is not a bug in Chrome, but incorrect usage of the compare function in the assignment text, this has now been fixed (site is not updated yet). The comparefn is supposed to return numeric values: 0 (a must be equal to b) 1 (a is greater than b by the ordering criterion) -1 (a is less than b by some ordering criterion) If the comparefn does not return numeric values it is not consistent and the sort order is implementation-defined (which is spec compliant). ascendingOrder (return a - b) version is not meant to work with strings, in any browser. It is an example of using a compare function for sorting an array of numbers using subtraction. You seem to be mixing the number (ascendingOrder) and string versions (alphabeticalOrder/reverseAlpha), not sure why. Your screenshot and table are confusing and as far as I can tell must be wrong. The screenshot shows Chrome failing to sort an array of numbers, using ascendingOrder, which is incorrect. Then your table shows Firefox passing on an array of strings, using ascendingOrder, which it does not. For both browsers, using ascendingOrder on an array of numbers sorts the array correctly. Using ascendingOrder in Chrome on an array of strings leaves the array unchanged. Using ascendingOrder in Firefox on an array of strings reverses the array but does not sort it. Just to be clear, ascendingOrder is meant to sort the array, not reverse it. It just happens to be shown in the example text on an array of numbers that is in reverse order and so sorting the array effectively reverses it. can't put the alphabeticalOrder version in the assignment text, that is the solution to the assignment. That is why I asked. deeply nested ternaries your last style example is pretty common, however, for what we have in the assignment I think a single line expression is better. My format suggestion is based on how prettier formats the code. Last I'm not sure if this conversation on a closed issue is considered proper etiquette or not, I simply don't have enough experience.
The above analysis is spot on. Thank you for the great explanation and its fine to have a discussion in a closed issue as long as its on topic. Otherwise you can always create new issue.
function alphabeticalOrder(arr) { return ((a, b) =a !== b ? (a < b ? -1 : 1) : 0); }

Discussion:
<section id='description'> The <code>sort</code> method sorts the elements of an array according to the callback function. For example: <del> <blockquote>function ascendingOrder(arr) {<br>&nbsp;&nbsp;return arr.sort(function(a, b) {<br>&nbsp;&nbsp;&nbsp;&nbsp;return a - b;<br>&nbsp;&nbsp;});<br>}<br>ascendingOrder([1, 5, 2, 3, 4]);<br>// Returns [1, 2, 3, 4, 5]<br><br>function reverseAlpha(arr) {<br>&nbsp;&nbsp;return arr.sort(function(a, b) {<br>&nbsp;&nbsp;&nbsp;&nbsp;return a < b;<br>&nbsp;&nbsp;});<br>}<br>reverseAlpha(['l', 'h', 'z', 'b', 's']);<br>// Returns ['z', 's', 'l', 'h', 'b']</blockquote> </del> <ins> <blockquote>function ascendingOrder(arr) {<br>&nbsp;&nbsp;return arr.sort(function(a, b) {<br>&nbsp;&nbsp;&nbsp;&nbsp;return a - b;<br>&nbsp;&nbsp;});<br>}<br>ascendingOrder([1, 5, 2, 3, 4]);<br>// Returns [1, 2, 3, 4, 5]<br><br>function reverseAlpha(arr) {<br>&nbsp;&nbsp;return arr.sort(function(a, b) {<br>&nbsp;&nbsp;&nbsp;&nbsp;return a === b ? 0 : a < b ? : 1 : -1;<br>&nbsp;&nbsp;});<br>}<br>reverseAlpha(['l', 'h', 'z', 'b', 's']);<br>// Returns ['z', 's', 'l', 'h', 'b']</blockquote> </ins> Note: It's encouraged to provide a callback function to specify how to sort the array items. JavaScript's default sorting method is by string Unicode point value, which may return unexpected results. </section>