Skip to content

Commit 5af094b

Browse files
updated
1 parent 5b0d79f commit 5af094b

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
//1) Create a function with an array of numbers as its parameter. The function should iterate through the array and return the minimum value from the array. Hint: Use what you know about if statements to identify and store the smallest value within the array.
2+
function findMiniValue(arr) {
3+
let minimum = arr[0];
24

5+
for (let i = 0; i < arr.length; i++) {
6+
if (arr[i] < minimum) {
7+
minimum = arr[i];
8+
}
9+
}
10+
11+
12+
13+
return minimum;
14+
}
315
//Sample arrays for testing:
416
let nums1 = [5, 10, 2, 42];
517
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
618
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
719

820
//Using one of the test arrays as the argument, call your function inside the console.log statement below.
921

10-
console.log(/* your code here */);
22+
console.log(findMiniValue(nums1));
23+
console.log(findMiniValue(nums2));
24+
console.log(findMiniValue(nums3));
25+

more-on-functions/studio/part-three-number-sorting-easy-way.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@ let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
44
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
55

66
//Sort each array in ascending order.
7-
7+
console.log(nums1.sort((a, b) => a - b));
8+
console.log(nums2.sort((a, b) => a - b));
9+
console.log(nums3.sort((a, b) => a - b));
810
//Sort each array in descending order.
11+
console.log(nums1.sort((a, b) => b - a));
12+
console.log(nums2.sort((a, b) => a - b));
13+
console.log(nums3.sort((a, b) => a - b));

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,43 @@ function findMinValue(arr){
1919
6) Be sure to print the results in order to verify your code.*/
2020

2121
//Your function here...
22+
function sortArray(unsortedArray){
23+
let newArray =[];
2224

25+
while(unsortedArray.length !== 0) {
26+
let smallestValue = findMinValue(unsortedArray);
27+
newArray.push(smallestValue);
28+
unsortedArray.splice(unsortedArray.indexOf(smallestValue),1);
29+
30+
}
31+
return newArray;
32+
}
2333
/* BONUS MISSION: Refactor your sorting function to use recursion below:
2434
*/
35+
function recursiveSortArray(unsortedArray, newArray = []) {
36+
if(unsortedArray.length === 1) {
37+
newArray.push(unsortedArray[0]);
38+
} else {
39+
newArray.push(findMinValue(unsortedArray));
40+
unsortedArray.splice(unsortedArray.indexOf(findMinValue(unsortedArray)), 1);
41+
recursiveSortArray(unsortedArray,newArray);
42+
}
43+
44+
45+
46+
return newArray;
47+
}
48+
49+
50+
51+
52+
2553

2654
//Sample arrays for testing:
2755
let nums1 = [5, 10, 2, 42];
2856
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
2957
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
58+
59+
console.log(recursiveSortArray(nums1));
60+
console.log(recursiveSortArray(nums2));
61+
console.log(recursiveSortArray(nums3));

0 commit comments

Comments
 (0)