본문 바로가기
알고리즘/Codewars

[Codewars] Sort Numbers (7 kyu) / JavaScript

by fluss 2023. 3. 28.

https://www.codewars.com/kata/5174a4c0f2769dd8b1000003

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

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 []
 

코드

function solution(nums){
  return (nums || []).sort((a, b) => a - b);
}

댓글