Skip to content

Commit 80e9e38

Browse files
committed
Studio More Functions
1 parent b0535f1 commit 80e9e38

File tree

3 files changed

+85
-27
lines changed

3 files changed

+85
-27
lines changed
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
//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-
3-
//Sample arrays for testing:
4-
let nums1 = [5, 10, 2, 42];
5-
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
6-
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
7-
8-
//Using one of the test arrays as the argument, call your function inside the console.log statement below.
9-
10-
console.log(/* your code here */);
1+
function findMinValue(arr) {
2+
if (arr.length === 0) {
3+
return undefined; // Handle the case where the array is empty
4+
}
5+
6+
let minValue = arr[0]; // Assume the first element is the minimum
7+
8+
for (let i = 1; i < arr.length; i++) {
9+
if (arr[i] < minValue) {
10+
minValue = arr[i]; // Update minValue if a smaller element is found
11+
}
12+
}
13+
14+
return minValue;
15+
}
16+
17+
// Sample arrays for testing:
18+
let nums1 = [5, 10, 2, 42];
19+
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
20+
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
21+
22+
// Using one of the test arrays as the argument, call your function inside the console.log statement below.
23+
console.log(findMinValue(nums1)); // Output should be 2
24+

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ let nums1 = [5, 10, 2, 42];
33
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

6-
//Sort each array in ascending order.
6+
nums1.sort((a, b) => a - b); // Sort nums1 in ascending order
7+
nums2.sort((a, b) => a - b); // Sort nums2 in ascending order
8+
nums3.sort((a, b) => a - b); // Sort nums3 in ascending order
9+
10+
console.log("Sorted in ascending order:");
11+
console.log(nums1); // [2, 5, 10, 42]
12+
console.log(nums2); // [-44, -10, -2, 0, 0, 3, 3, 5]
13+
console.log(nums3); // [-3.3, 0, 4.4, 4, 5, 5, 8, 10, 200]
14+
715

816
//Sort each array in decending order.
17+
nums1.sort((a, b) => b - a); // Sort nums1 in descending order
18+
nums2.sort((a, b) => b - a); // Sort nums2 in descending order
19+
nums3.sort((a, b) => b - a); // Sort nums3 in descending order
20+
21+
console.log("Sorted in descending order:");
22+
console.log(nums1); // [42, 10, 5, 2]
23+
console.log(nums2); // [5, 3, 3, 0, 0, -2, -10, -44]
24+
console.log(nums3); // [200, 10, 8, 5, 5, 4.4, 4, 0, -3.3]
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
1-
function findMinValue(arr){
1+
function findMinValue(arr) {
22
let min = arr[0];
3-
for (i = 0; i < arr.length; i++){
4-
if (arr[i] < min){
3+
for (let i = 0; i < arr.length; i++) {
4+
if (arr[i] < min) {
55
min = arr[i];
66
}
77
}
88
return min;
99
}
1010

11-
//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.
11+
function sortArray(arr) {
12+
let sortedArray = [];
1213

13-
/*Within the function:
14-
1) Define a new, empty array to hold the final sorted numbers.
15-
2) Use the findMinValue function to find the minimum value in the old array.
16-
3) Add the minimum value to the new array, and remove the minimum value from the old array.
17-
4) Repeat parts b & c until the old array is empty.
18-
5) Return the new sorted array.
19-
6) Be sure to print the results in order to verify your code.*/
20-
21-
//Your function here...
14+
while (arr.length > 0) {
15+
let minValue = findMinValue(arr);
16+
sortedArray.push(minValue);
17+
arr.splice(arr.indexOf(minValue), 1);
18+
}
2219

23-
/* BONUS MISSION: Refactor your sorting function to use recursion below:
24-
*/
20+
return sortedArray;
21+
}
2522

26-
//Sample arrays for testing:
23+
// Sample arrays for testing:
2724
let nums1 = [5, 10, 2, 42];
2825
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
2926
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
27+
28+
console.log("Sorted nums1:", sortArray(nums1)); // [2, 5, 10, 42]
29+
console.log("Sorted nums2:", sortArray(nums2)); // [-44, -10, -2, 0, 0, 3, 3, 5]
30+
console.log("Sorted nums3:", sortArray(nums3)); // [-3.3, 0, 4.4, 4, 5, 5, 8, 10, 200]
31+
32+
33+
34+
function findMinValue(arr) {
35+
let min = arr[0];
36+
for (let i = 0; i < arr.length; i++) {
37+
if (arr[i] < min) {
38+
min = arr[i];
39+
}
40+
}
41+
return min;
42+
}
43+
44+
function sortArray(arr) {
45+
if (arr.length === 0) {
46+
return [];
47+
}
48+
49+
let minValue = findMinValue(arr);
50+
arr.splice(arr.indexOf(minValue), 1);
51+
52+
return [minValue].concat(sortArray(arr));
53+
}
54+
55+
console.log("Sorted nums1:", sortArray(nums1)); // [2, 5, 10, 42]
56+
console.log("Sorted nums2:", sortArray(nums2)); // [-44, -10, -2, 0, 0, 3, 3, 5]
57+
console.log("Sorted nums3:", sortArray(nums3)); // [-3.3, 0, 4.4, 4, 5, 5, 8, 10, 200]

0 commit comments

Comments
 (0)