Sort Numbers

Lee·2022년 8월 14일

Algorithm

목록 보기
70/92
post-thumbnail

❓ Sort Numbers

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

✔ Solution

//#my solution

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


//#other solution
function solution(nums){
  return (nums || []).sort(function(a, b){
    return a - b
  });
}
profile
Lee

0개의 댓글