I feel like I keep popping on here with increasingly boring questions, but I am stumped. Why am I getting an undefined log on the console for this?
const sortSpeciesByTeeth = arr => {
arr.sort((a, b) => a.numTeeth - b.numTeeth
)
};
The only thing I can figure is that it’s my a and b variables, but I’m not sure how to define them given that they’re referring to the various objects in the array.
Thank you for any guidance you can give.
Hello! Could you post the full code, and the exact error, please?
Sure, of course:
const speciesArray = [ {speciesName:'shark', numTeeth:50}, {speciesName:'dog', numTeeth:42}, {speciesName:'alligator', numTeeth:80}, {speciesName:'human', numTeeth:32}];
const sortSpeciesByTeeth = arr => {
arr.sort((a, b) => a.numTeeth - b.numTeeth
)
};
This was for the Intermediate JavaScript challenges. The goal is to sort the species listed by the numTeeth value. It keeps returning undefined and I can’t figure out why. I
In the exercise, I changed the operator from the - to the > and it functions to sort, but I don’t understand why this code as-is returns undefined.
Sorry for the really late reply; I’ve been away from the screen for a while. I’ll try to get back to you with a real answer soon, if you’re still interested.
Not to worry; I am still interested, but I understand that life can get in the way! Thank you for getting back to me 
1 Like
Could you post the code that works for you, and the output, please? When I run it with the -
operator, it works, where it doesn’t when I use the >
operator…
type or paste code here
```const speciesArray = [ {speciesName:'shark', numTeeth:50}, {speciesName:'dog', numTeeth:42}, {speciesName:'alligator', numTeeth:80}, {speciesName:'human', numTeeth:32}];
// Write your code here:
const sortSpeciesByTeeth = arr =>
arr.sort((a, b) => a.numTeeth > b.numTeeth
);
console.log(sortSpeciesByTeeth(speciesArray))
Prints, as requested:
// [ { speciesName: 'human', numTeeth: 32 },
// { speciesName: 'dog', numTeeth: 42 },
// { speciesName: 'shark', numTeeth: 50 },
// { speciesName: 'alligator', numTeeth: 80 } ]
I tried the minus again after you said it was working, and it worked this time. I was sure I just changed the operator…how frustrating.
Thank you so much for chasing this down and trying. Seems like the moral of the story is that it works fine and I just need to read closer?
1 Like