You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: more-on-functions/studio/part-two-create-sorted-array.js
+28Lines changed: 28 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,21 @@ function findMinValue(arr){
7
7
}
8
8
returnmin;
9
9
}
10
+
functionminValueArrayIndex(myArray){
11
+
letminValueIndex=0;
12
+
letminValue=myArray[0];
13
+
for(leti=1;i<myArray.length;i++){
14
+
if(minValue>myArray[i]){
15
+
minValue=myArray[i];
16
+
minValueIndex=i;
17
+
}
10
18
19
+
}
20
+
returnminValueIndex;
21
+
22
+
23
+
}
24
+
11
25
//Create a function with an array of numbers as its parameter. This function will return a new array with the numbers sorted from least to greatest value.
12
26
13
27
/*Within the function:
@@ -19,6 +33,19 @@ function findMinValue(arr){
19
33
6) Be sure to print the results in order to verify your code.*/
20
34
21
35
//Your function here...
36
+
functionsortArray(arr){
37
+
letsortedArray=[];
38
+
letminValue;
39
+
while(arr.length>0){
40
+
41
+
letminValueIndex=minValueArrayIndex(arr);
42
+
minValue=findMinValue(arr);
43
+
sortedArray.push(minValue)
44
+
arr.splice(minValueIndex,1);
45
+
46
+
}
47
+
returnsortedArray;
48
+
}
22
49
23
50
/* BONUS MISSION: Refactor your sorting function to use recursion below:
0 commit comments