Sort an Array Alphabetically using the sort Method explain when the arr is sorted

Sort an Array Alphabetically using the sort Method explain when the arr is sorted
0

Tell us what’s happening:



**Your code so far**

```js

function alphabeticalOrder(arr) {
  // Add your code below this line
  
    return arr.sort(function(a,b)
    {
      console.log(arr);
      console.log(a);
      console.log(b);
      console.log(arr);
      if(a>=b)
      return 1;
      else
      return -1;
     
      
    })
  
}
console.log(alphabeticalOrder(["x", "h", "a", "m", "n", "m"]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method

You don’t tell us what is your problem or question.

Your code passes all the tests, just remove the console.log.

From console.log(alphabeticalOrder(["x", "h", "a", "m", "n", "m"])); to alphabeticalOrder(["x", "h", "a", "m", "n", "m"]);.

When I try to console.log the array //console.log(arr) it doesn't show the array as it is being sorted. Why is this?

do the console.log outside of the sort callback function not in the function while it is being sorted and you will see the ordered arr for example:

function alphabeticalOrder(arr) {
  arr.sort(function(a,b) {
    if(a>=b)
      return 1;
    else
      return -1;
  })
  console.log('after the sort', arr)
  return arr
}
alphabeticalOrder(["x", "h", "a", "m", "n", "m"]);