I've been trying and trying different ways to think outside the box on this one and for some reason I can't get it.
function sortByLength(array) {
for(var i = 0; i < array.length; i++) {
for(var n = 0; n < array.length; n++) {
array.sort(function(i, n) {
return i.length - n.length;
});
}
}
return array;
}
console.log(sortByLength(["Hello", "Cheese", "Bye"]));
//expecting ["Bye", "Hello", "Cheese"]
I guess I'm trying to figure out:
1. Why is this an infinite loop?
2. Why can't I simply pass in the looped values of i and n, then compare them to sort them by length?
Any clues or help I can get will be much appreciated. Thanks!