DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Edited on

3 3

#20 - Sort Numbers CodeWars Kata (7 kyu)

Instructions

Finish the solution so that it sorts the passed in array of numbers. If the function passes in an empty array or null/nil value then it should return an empty array.

For example:

solution([1, 2, 10, 50, 5]); // should return [1,2,5,10,50]
solution(null); // should return []


My solution:

function solution(nums){
  return nums !== null ? nums.sort((a,b)=> a-b) : [] 
}
Enter fullscreen mode Exit fullscreen mode

Explanation

I returned a value using a conditional, if the nums array isn't null it will sort the nums array using a comparison function, it will rest a-b so it returns the nums array sorted correctly and if the element is null it will return an empty array


Did you like this solution? 👇🤔

My Github
My twitter
Solve this Kata

Top comments (0)

AI Agent image

How to Build an AI Agent with Semantic Kernel (and More!)

Join Developer Advocate Luce Carter for a hands-on tutorial on building an AI-powered dinner recommendation agent. Discover how to integrate Microsoft Semantic Kernel, MongoDB Atlas, C#, and OpenAI for ingredient checks and smart restaurant suggestions.

Watch the video 📺

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay