Skip to content

Commit 5bd928a

Browse files
committed
morefunctions
1 parent 85473af commit 5bd928a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

more-on-functions/studio/part-two-create-sorted-array.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@ function findMinValue(arr){
77
}
88
return min;
99
}
10+
function minValueArrayIndex(myArray){
11+
let minValueIndex = 0;
12+
let minValue =myArray[0];
13+
for(let i=1;i<myArray.length;i++){
14+
if(minValue>myArray[i]){
15+
minValue=myArray[i];
16+
minValueIndex =i;
17+
}
1018

19+
}
20+
return minValueIndex;
21+
22+
23+
}
24+
1125
//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.
1226

1327
/*Within the function:
@@ -19,6 +33,19 @@ function findMinValue(arr){
1933
6) Be sure to print the results in order to verify your code.*/
2034

2135
//Your function here...
36+
function sortArray (arr){
37+
let sortedArray =[];
38+
let minValue ;
39+
while(arr.length>0){
40+
41+
let minValueIndex =minValueArrayIndex(arr);
42+
minValue =findMinValue(arr);
43+
sortedArray.push(minValue)
44+
arr.splice(minValueIndex,1);
45+
46+
}
47+
return sortedArray;
48+
}
2249

2350
/* BONUS MISSION: Refactor your sorting function to use recursion below:
2451
*/
@@ -27,3 +54,4 @@ function findMinValue(arr){
2754
let nums1 = [5, 10, 2, 42];
2855
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
2956
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
57+
console.log(sortArray(nums1));

0 commit comments

Comments
 (0)