Sort Ascending Order

Ayumi Tanaka
Algorithm Novice
Published in
Sep 30, 2020

Q. Create a function that takes an array of numbers and returns a new array, sorted in ascending order (smallest to biggest).

Sort numbers array in ascending order.

If the function’s argument is null, an empty array, or undefined; return an empty array.

Return a new array of sorted numbers.

const sortNumsAscending = function(arr) { if (!arr || arr.length === 0) {  console.log('[]'); } else {  console.log(arr.sort((a, b) => a - b)); }}sortNumsAscending([1, 2, 10, 50, 5]) // should be [1, 2, 5, 10, 50]sortNumsAscending(null) // should be []sortNumsAscending([]) // should be []
  1. Return [] if the array is not an array or the array doesn’t have any elements. (Which means array.length should be 0.)
  2. Otherwise, rearrange elements in ascending order with sort() method.

--

--