TypeError: n.sort is not a function [duplicate]

I cannot figure out how to use .sort inside of a function

When I try to call the function below I get the TypeError: n.sort is not a function

n=[1, 4, 3]
function descendingOrder(n){
  n.sort(function(a, b){return b-a})
}

However, when I just use n.sort(function(a, b){return b-a}) without the descendingOrderfunction everything works fine.

What is wrong with my first approach?

  • 1

    Please include all relevant code. How are you calling descendingOrder? Note that the n in descendingOrder(n) is not the same as the n in n=[...]

    – 

  • How are you calling descendingOrder()? You initalize n as an array, but you are also using that variable name as a parameter.

    – 

  • Quick fix would be to change function descendingOrder(n){ to function descendingOrder() { or where you call it from descendingOrder(); to descendingOrder(n); – but that won’t help you understand why, which is more important.

    – 




Leave a Comment