Tell us what’s happening:
I learnt that sort method mutates an array that is sorted and that one way to avoid this is to concatenate an empty into the being sorted using either slice() or concat() method.
pls can anyone explain to me why this doesn’t work with alphabetical array but does with number array?
for example the code below gives the same output :
‘’’
[“y”, “x”, “u”, “d”, “b”, “a”, “a”]
[“y”, “x”, “u”, “d”, “b”, “a”, “a”]
‘’’
I thought consoleloging alpha should output the original array. But it didn’t. why?
pls I am highly sorry for the mistakes in my first post.
Your code so far
var alpha = ["a", "d", "c", "a", "z", "g"]
function alphabeticalOrder(arr) {
// Add your code below this line
var newArr = arr.sort();
return newArr.sort(function(a,b){
return a<b ? -1 : 1
})
// Add your code above this line
}
console.log(alphabeticalOrder(alpha));
console.log(alpha);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36
.
Challenge: Sort an Array Alphabetically using the sort Method
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method