Need help with Codecademy challenge


This is the challenge I’m trying to solve:

Write a function sortSpeciesByTeeth() that takes in an array of species objects in the format:

{speciesName: 'shark', numTeeth: 50 }

and sorts the array in ascending order based on the average number of teeth that species possesses ( numTeeth ) .

You’ll need to access each object’s .numTeeth property. Feel free to either write a named comparison function, or use an anonymous function for your argument to .sort() .

You can test your function when you’re ready by passing in the speciesArray array or by making your own!

Here is my code:

const speciesArray = [ {speciesName:'shark', numTeeth:50}, {speciesName:'dog', numTeeth:42}, {speciesName:'alligator', numTeeth:80}, {speciesName:'human', numTeeth:32}];

// Write your code here:
function sortSpeciesByTeeth(passedArray) {
  console.log(passedArray.sort(function(a, b){return a.numTeeth - b.numTeeth}));


It prints out this like it is supposed to:

[ { speciesName: 'human', numTeeth: 32 },
  { speciesName: 'dog', numTeeth: 42 },
  { speciesName: 'shark', numTeeth: 50 },
  { speciesName: 'alligator', numTeeth: 80 } ]

So how come it won’t let me pass the challenge? :thinking:

Thanks for any help!

Did the challenge specify that you should console.log instead of return?


That solved it, thanks! I didn’t return it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.