Skip to content

Commit 1c938f1

Browse files
committed
modified: more-on-functions/studio/part-one-find-minimum-value.js
modified: more-on-functions/studio/part-three-number-sorting-easy-way.js modified: more-on-functions/studio/part-two-create-sorted-array.js
1 parent bf7c717 commit 1c938f1

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
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.
22

3+
function smallNum(arr) {
4+
let prev = arr[0];
5+
arr.map((indiv) => {
6+
if (indiv < prev) {
7+
prev = indiv;
8+
}
9+
});
10+
return prev;
11+
}
12+
313
//Sample arrays for testing:
414
let nums1 = [5, 10, 2, 42];
515
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
616
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
717

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

10-
console.log(/* your code here */);
20+
console.log(smallNum(nums1));

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,16 @@ let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
55

66
//Sort each array in ascending order.
77

8+
function ascend(arr) {
9+
return arr.sort((a, b) => a - b);
10+
}
11+
12+
console.log(ascend(nums3));
13+
814
//Sort each array in descending order.
15+
16+
function descend(arr) {
17+
return arr.sort((a, b) => b - a);
18+
}
19+
20+
console.log(descend(nums3));

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

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

2121
//Your function here...
2222

23+
function newArr(arr) {
24+
let arrTwo = []
25+
26+
while (arr.length > 0) {
27+
let small = findMinValue(arr)
28+
arrTwo.push(small)
29+
arr.splice(arr.indexOf(small), 1)
30+
}
31+
32+
return arrTwo
33+
34+
}
35+
2336
/* BONUS MISSION: Refactor your sorting function to use recursion below:
2437
*/
2538

2639
//Sample arrays for testing:
2740
let nums1 = [5, 10, 2, 42];
2841
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
2942
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
43+
44+
45+
46+
console.log(newArr(nums3))

0 commit comments

Comments
 (0)