Skip to content

Commit 7fa0de9

Browse files
author
Edward King
committed
Studio 7/1 worked w/Caleb
1 parent 2848714 commit 7fa0de9

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
Lines changed: 12 additions & 0 deletions
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.
2+
function minValue(numArray) {
3+
let smallestValue = numArray[0];
4+
for (let i = 0; i < numArray.length; i++) {
5+
if (numArray[i] < smallestValue) {
6+
smallestValue = numArray[i];
7+
}
8+
}
9+
return smallestValue;
10+
}
211

312
//Sample arrays for testing:
413
let nums1 = [5, 10, 2, 42];
514
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
615
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
716

17+
console.log(minValue(nums1));
18+
console.log(minValue(nums2));
19+
console.log(minValue(nums3));
820
//Using one of the test arrays as the argument, call your function inside the console.log statement below.
921

1022
console.log(/* your code here */);

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ function findMinValue(arr){
99
}
1010

1111
//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.
12-
12+
function sortedNumbers (arr){
13+
let sortArray = [];
14+
for (i = 0;arr.length > 0;i++ ){
15+
if (arr[i] < min){
16+
min = arr[i]
17+
}
18+
}
19+
return min
20+
}
1321
/*Within the function:
1422
1) Define a new, empty array to hold the final sorted numbers.
1523
2) Use the findMinValue function to find the minimum value in the old array.
@@ -27,3 +35,7 @@ function findMinValue(arr){
2735
let nums1 = [5, 10, 2, 42];
2836
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
2937
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
38+
39+
console.log(sortedNumbers(nums1));
40+
console.log(sortedNumbers(nums2));
41+
console.log(sortedNumbers(nums3));

0 commit comments

Comments
 (0)