Skip to content

Commit 2ff1889

Browse files
committed
Objects and Math
1 parent 4a40166 commit 2ff1889

File tree

6 files changed

+123
-6
lines changed

6 files changed

+123
-6
lines changed

more-on-functions/exercises/raid-a-shuttle.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,3 @@ let irs = function(levelOfFuel, itemsInCargo) {
7070

7171
//c). Use a template literal to return, "Raided _____ kg of fuel from the tanks, and stole ____ and ____ from the cargo hold."
7272

73-
let irs = function(levelOfFuel, itemsInCargo {
74-
let arr = deckMops(itemsInCargo);
75-
return `Raided ${nonSuspiciousFunction(fuelLevel)} kg of fuel from the tanks, and stole ${arr[0]} and ${arr[1]} from the cargo hold.`
76-
}

more-on-functions/studio/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

more-on-functions/studio/part-one-find-minimum-value.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,22 @@ let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
88
//Using one of the test arrays as the argument, call your function inside the console.log statement below.
99

1010
console.log(/* your code here */);
11+
12+
13+
// Create a function with an array of numbers as its parameter
14+
function findMinimumValue(arr) {
15+
if (arr.length === 0) {
16+
return; // Return null if the array is empty
17+
}
18+
}
19+
20+
let minValue = arr[0]; // Assume the first element is the minimum
21+
22+
for (let i = 1; i < arr.length; i++) {
23+
if (arr[i] < minValue) {
24+
miniValue = arr[i]; // update minValue if a smaller value is found
25+
}
26+
}
27+
return minValue;
28+
29+
console.log(findMinimumValue(nums1)); // Output: 2

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,20 @@ 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+
let sortedNums1Asc = [...nums1].sort((a, b) => a - b);
8+
let sortedNums2Asc = [...nums2].sort((a, b) => a - b);
9+
let sortedNums3Asc = [...nums3].sort((a, b) => a - b);
10+
11+
console.log(sortedNums1Asc); // Output: [2, 5, 10, 42]
12+
console.log(sortedNums2Asc); // Output: [-44, -10, -2, 0, 0, 3, 3, 5]
13+
console.log(sortedNums3Asc); // Output: [-3.3, 0, 4, 4.4, 5, 5, 8, 10, 200]
14+
715

816
//Sort each array in descending order.
17+
let sortedNums1Desc = [...nums1].sort((a, b) => b - a);
18+
let sortedNums2Desc = [...nums2].sort((a, b) => b - a);
19+
let sortedNums3Desc = [...nums3].sort((a, b) => b - a);
20+
21+
console.log(sortedNums1Desc); // Output: [42, 10, 5, 2]
22+
console.log(sortedNums2Desc); // Output: [5, 3, 3, 0, 0, -2, -10, -44]
23+
console.log(sortedNums3Desc); // Output: [200, 10, 8, 5, 5, 4.4, 4, 0, -3.3]

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,39 @@ function findMinValue(arr){
2020

2121
//Your function here...
2222

23+
// Function to find the minimum value in an array
24+
function findMinValue(arr) {
25+
let min = arr[0];
26+
for (let i = 1; i < arr.length; I++) {
27+
if (arr[i] < min) {
28+
min = arr[i]
29+
}
30+
}
31+
return min;
32+
}
33+
34+
//Function to create A SORTED array from an input array
35+
function createSortedArray(arr) {
36+
let soretedArray = [];
37+
let orginalArray = [...arr]; // Make a copy of orginal array
38+
while (orginalArray.length > 0) {
39+
let minValue = findMinValue(orignalArray);
40+
soretedArray.push(minValue);
41+
42+
originalArray.splice(orginalArray.indexOf(minValue), 1); // Remove the minvalue from the original array
43+
}
44+
return soretedArray;
45+
}
2346
/* BONUS MISSION: Refactor your sorting function to use recursion below:
2447
*/
2548

2649
//Sample arrays for testing:
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+
55+
// Testing the function
56+
console.log(createSortedArray(nums1)); // Output: [2, 5, 10, 42]
57+
console.log(createSortedArray(nums2)); // Output: [-44, -10, -2, 0, 0, 3, 3, 5]
58+
console.log(createSortedArray(nums3)); // Output: [-3.3, 0, 4, 4.4, 5, 5, 8, 10, 200]

objects-and-math/exercises/ObjectExercises.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,50 @@ let superChimpOne = {
22
name: "Chad",
33
species: "Chimpanzee",
44
mass: 9,
5-
age: 6
5+
age: 6,
6+
astronautID: 1,
7+
move: function () {return Math.floor(Math.random()*11)}
68
};
79

810
let salamander = {
911
name: "Lacey",
1012
species: "Axolotl Salamander",
1113
mass: 0.1,
12-
age: 5
14+
age: 5,
15+
astronautID: 2,
16+
move: function () {return Math.floor(Math.random()*11)}
1317
};
1418

19+
let superChimpTwo = {
20+
name: "Brad",
21+
species: "Chimpanzee",
22+
mass: 11,
23+
age: 6,
24+
astronautID: 3,
25+
move: function () {return Math.floor(Math.random()*11)}
26+
};
27+
28+
let dog = {
29+
name: "Leroy",
30+
species: "Beagle",
31+
mass: 14,
32+
age: 5,
33+
astronautID: 4,
34+
move: function () {return Math.floor(Math.random()*11)}
35+
};
36+
37+
let waterBear = {
38+
name: "Almina",
39+
species: "Tardigrade",
40+
mass: 0.0000000001,
41+
age: 1,
42+
astronautID: 5,
43+
move: function () {return Math.floor(Math.random()*11)}
44+
};
45+
46+
let crew = [superChimpOne, superChimpTwo, salamander, dog, waterBear];
47+
48+
1549

1650
// After you have created the other object literals, add the astronautID property to each one.
1751

@@ -22,3 +56,21 @@ let salamander = {
2256
// Print out the relevant information about each animal.
2357

2458
// Start an animal race!
59+
60+
function fitnessTest(candidates){
61+
let results = [], numSteps, turns;
62+
63+
for (let i = 0; i < candidates.length; i++){
64+
numSteps = 0;
65+
turns = 0;
66+
67+
while(numSteps < 20){
68+
numSteps += candidates[i].move();
69+
turns++;
70+
}
71+
72+
results.push(`${candidates[i].name} took ${turns} turns to take 20 steps.`);
73+
}
74+
75+
return results;
76+
}

0 commit comments

Comments
 (0)