Write a min function that returns the smallest number in the array numbers.
The following uses arrays [12,5,8,7,9,0,2] as an example
let min = (numbers) => { if (numbers.length > 2) { return min([numbers[0], min(numbers.slice(1))]); } else { return Math.min.apply(null, numbers); }}; console.log(min([12, 5, 8, 7, 9, 0, 2]));Copy the code
Write a sort function that requires that sort(numbers) return an array of numbers from smallest to largest (you can add extra helper functions).
The following uses arrays [12,5,8,7,9,0,2] as an example
let minIndex = (numbers) => numbers.indexOf(min(numbers)); let min = (numbers) => { if (numbers.length > 2) { return min([numbers[0], min(numbers.slice(1))]); } else { return Math.min.apply(null, numbers); }}; let sort = (numbers) => { if (numbers.length > 2) { let index = minIndex(numbers); let min = numbers[index]; numbers.splice(index, 1); return [min].concat(sort(numbers)); } else { return numbers[0] < numbers[1] ? numbers : numbers.reverse(); }}; console.log(sort([12, 5, 8, 7, 9, 0, 2]));Copy the code