Skip to content

Commit c7896e9

Browse files
committed
studio workon functions
1 parent 2dc8cb7 commit c7896e9

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

more-on-functions/studio/part-one-find-minimum-value.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,17 @@ let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
77

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

10-
console.log(/* your code here */);
10+
function findMin(arr){
11+
let min = arr[0];
12+
13+
for(i = 1; i<arr.length; i++){
14+
if (arr[i]<min){
15+
min = arr[i];
16+
}
17+
18+
} return min;
19+
}
20+
21+
console.log(findMin(nums1));
22+
console.log(findMin(nums2));
23+
console.log(findMin(nums3));

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,23 @@ 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+
console.log("Part 3 - sort()");
78

9+
let num3A = [5, 10, 2, 42];
10+
num3A.sort(function (a, b){
11+
return a-b;
12+
});
13+
14+
15+
let num3B = [-2, 0, -10, -44, 5, 3, 0, 3];
16+
num3B.sort(function(b,a){
17+
return a-b;
18+
});
19+
20+
21+
22+
let num3C = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
23+
num3C.sort(function (a,b) {
24+
return b-a;
25+
})
826
//Sort each array in decending order.

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,21 @@ function findMinValue(arr){
2020

2121
//Your function here...
2222

23+
let nums1 = [5, 10, 2, 42];
24+
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
25+
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
26+
27+
function sortNum(arr){
28+
let sortedArray = [];
29+
while(arr.length>0){
30+
let min = findMin(arr);
31+
sortedArray.push(min);
32+
arr = arr.filter(num=> num !== min);
33+
}
34+
} return sortedArray;
35+
2336
/* BONUS MISSION: Refactor your sorting function to use recursion below:
2437
*/
2538

2639
//Sample arrays for testing:
27-
let nums1 = [5, 10, 2, 42];
28-
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
29-
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
40+

0 commit comments

Comments
 (0)