Skip to content

Commit 52c4d57

Browse files
committed
more on functions studio
1 parent b9307ff commit 52c4d57

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
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+
const minValue = function (arr) {
4+
5+
let minValue = arr[0];
6+
for (let i = 0; i < arr.length; i++) {
7+
if (arr[i] < minValue) {
8+
minValue = arr[i];
9+
}
10+
}
11+
12+
return minValue;
13+
};
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(minValue(nums3));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ 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+
let testArr = nums1;
7+
68
//Sort each array in ascending order.
9+
let ascending = testArr.sort(function(a, b){return a - b});
10+
console.log(ascending);
711

812
//Sort each array in decending order.
13+
let decending = testArr.sort(function(a, b){return b - a});
14+
console.log(decending);
15+
16+
//Yes it does alter the array.

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

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

2121
//Your function here...
22+
const sortedArr = function (arr) {
23+
let sorted = [];
24+
while (arr.length !== 0) {
25+
sorted.push(findMinValue(arr));
26+
arr.splice(arr.indexOf(findMinValue(arr)), 1);
27+
}
28+
return sorted;
29+
};
30+
31+
32+
// Using Recursion
33+
// let sorted = [];
34+
// const sortedArr = function (arr) {
35+
// if (arr.length === 0) {
36+
// console.log(`base reached`);
37+
// return sorted;
38+
// } else {
39+
// sorted.push(findMinValue(arr));
40+
// arr.splice(arr.indexOf(findMinValue(arr)), 1);
41+
// //console.log(`arr is ${arr}`);
42+
// return sortedArr(arr);
43+
// }
44+
// };
2245

2346
/* BONUS MISSION: Refactor your sorting function to use recursion below:
2447
*/
@@ -27,3 +50,5 @@ function findMinValue(arr){
2750
let nums1 = [5, 10, 2, 42];
2851
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
2952
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
53+
54+
console.log(sortedArr(nums2));

0 commit comments

Comments
 (0)