MCPcopy Create free account
hub / github.com/appacademy/Module-1-Resources / sort

Function sort

w3/d2/sort.js:24–49  ·  view source on GitHub ↗

Write a recursive function called `sort` that takes an array of integers, `nums` and returns an array containing those integers sorted from least to greatest. Your function should accept a default argument called `sorted` which holds the currently sorted elements. Each recursive step should add the

(nums)

Source from the content-addressed store, hash-verified

22***********************************************************************/
23
24function sort(nums) {
25 // 1. Check the base case: If `nums` is empty, then return []
26 if(!nums.length) return [];
27
28// 2. Otherwise, find the smallest element in `nums`
29// let largestNum = -Infinity;
30// let largestI = 0;
31// for(let i = 0; i < nums.length; i++){
32// let num = nums[i];
33// // console.log(num);
34// if(largestNum < num) {
35// largestNum = num;
36// largestI = i
37// }
38// }
39 let largestNum = Math.max(...nums);
40 let index = nums.indexOf(largestNum);
41
42 nums.splice(index, 1)
43 let largest = [];
44 largest.push(largestNum)
45
46 sort(nums) + sort(nums)
47 return sort(nums).concat(largest)
48 // return [...sort(nums), largestNum];
49}
50/*
51
52sort([]) => []

Callers 1

sort.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected