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?
Please include all relevant code. How are you calling
descendingOrder
? Note that then
indescendingOrder(n)
is not the same as then
inn=[...]
How are you calling
descendingOrder()
? You initalizen
as an array, but you are also using that variable name as a parameter.Quick fix would be to change
function descendingOrder(n){
tofunction descendingOrder() {
or where you call it fromdescendingOrder();
todescendingOrder(n);
– but that won’t help you understand why, which is more important.