Skip to content

Commit 37a763b

Browse files
committed
finished
1 parent db9b4e0 commit 37a763b

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
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 findMinimum(arr){
3+
minimum = Infinity
4+
5+
6+
for (i = 0 ; i < arr.length; i++){
7+
if (arr[i]< minimum){
8+
9+
minimum = arr[i]
10+
11+
}
12+
13+
}
14+
15+
return minimum
16+
17+
}
18+
19+
20+
221

322
//Sample arrays for testing:
423
let nums1 = [5, 10, 2, 42];
@@ -7,4 +26,4 @@ let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
726

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

10-
console.log(/* your code here */);
29+
console.log(findMinimum(nums3));

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,61 @@
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];
5-
5+
function findMinValue(arr){
6+
let min = arr[0];
7+
8+
for (i = 0; i < arr.length; i++){
9+
if (arr[i] < min){
10+
min = arr[i];
11+
}
12+
13+
}
14+
15+
return min;
16+
17+
}
18+
function sortArray(arr){
19+
let sorted = [];
20+
let arrCopy= []
21+
arrCopy= arr;
22+
while(arrCopy.length !=0){
23+
let min = findMinValue(arr)
24+
sorted.push(min)
25+
arrCopy.splice(arrCopy.indexOf(min),1)
26+
}
27+
return sorted
28+
}
629
//Sort each array in ascending order.
730

31+
console.log(sortArray(nums1))
32+
console.log(sortArray(nums2))
33+
console.log(sortArray(nums3))
834
//Sort each array in decending order.
35+
36+
function findMaxValue(arr){
37+
let max = arr[0];
38+
39+
for (i = 0; i < arr.length; i++){
40+
if (arr[i] > max){
41+
max = arr[i]
42+
}
43+
44+
}
45+
46+
return max;
47+
48+
}
49+
50+
function sortArrayDescending(arr){
51+
let sorted = [];
52+
while(arr.length !=0){
53+
let max = findMaxValue(arr)
54+
sorted.push(max)
55+
arr.splice(arr.indexOf(max),1)
56+
}
57+
return sorted
58+
}
59+
60+
console.log(sortArrayDescending(nums1))
61+
console.log(sortArrayDescending(nums2))
62+
console.log(sortArrayDescending(nums3))

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
function findMinValue(arr){
22
let min = arr[0];
3+
34
for (i = 0; i < arr.length; i++){
45
if (arr[i] < min){
56
min = arr[i];
6-
}
7+
}
8+
79
}
10+
811
return min;
12+
913
}
1014

1115
//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-
16+
function sortArray(arr){
17+
let sorted = [];
18+
while(arr.length !=0){
19+
let min = findMinValue(arr)
20+
sorted.push(min)
21+
arr.splice(arr.indexOf(min),1)
22+
}
23+
return sorted
24+
}
1325
/*Within the function:
1426
1) Define a new, empty array to hold the final sorted numbers.
1527
2) Use the findMinValue function to find the minimum value in the old array.
@@ -27,3 +39,4 @@ function findMinValue(arr){
2739
let nums1 = [5, 10, 2, 42];
2840
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
2941
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
42+
console.log(sortArray(nums1))

0 commit comments

Comments
 (0)