Problem

_WorkOS gives you Lego bricks for adding enterprise features to your app. You can use their modern API’s to add stuff like Single Sign-On (SAML) and SCIM user provisioning in a few minutes, instead of a few months.

function addAndSort(array, element) {
  return array.push(element).sort();
}
Solution

When you call array.push on an array, it doesn’t return the updated array, but rather the length of the array with the new item. That means you can’t chain the .sort() call to the end, since the result of .push is a number.

There are two ways to fix this. One option is to call array.sort() after the .push call.

function addAndSort(array, element) {
  array.push(element)
  return array.sort();
}

The second option appends the item to a new array with array.concat() and then sorts the new array.

function addAndSort(array, element) {
  return array.concat(element).sort();
}