Skip to content

Commit 93f7fd0

Browse files
committed
Chapter 11 Studio
1 parent 4cdc687 commit 93f7fd0

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
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-
2+
function findMin (array){
3+
let minVal = array[0];
4+
for (let i = 0; i < array.length; i++){
5+
if (array[i] < minVal){
6+
minVal = array[i];
7+
}
8+
}
9+
return minVal;
10+
}
311
//Sample arrays for testing:
412
let nums1 = [5, 10, 2, 42];
513
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
614
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
715

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

10-
console.log(/* your code here */);
18+
console.log(findMin(nums3));

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ 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(nums2.sort(function(a, b){return a - b}));
78

89
//Sort each array in descending order.
10+
console.log(nums2.reverse());

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

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

2121
//Your function here...
22-
22+
function arraySort(array){
23+
let sortedArray = [];
24+
while (array.length > 0){
25+
sortedArray.push(findMinValue(array));
26+
array.splice(array.indexOf(findMinValue(array)), 1);
27+
}
28+
return sortedArray;
29+
}
2330
/* BONUS MISSION: Refactor your sorting function to use recursion below:
2431
*/
2532

2633
//Sample arrays for testing:
2734
let nums1 = [5, 10, 2, 42];
2835
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
2936
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
37+
38+
console.log(arraySort(nums3));

0 commit comments

Comments
 (0)