Skip to content

Commit adbedb1

Browse files
committed
Objects and Math exercises and examples
1 parent 52c4d57 commit adbedb1

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

objects-and-math/chapter-examples/ForInLoop.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,18 @@ let tortoiseOne = {
66
diet: ["pumpkins", "lettuce", "cabbage"]
77
};
88

9-
// Using a for..in loop, iterate through each property in the tortoiseOne object and print the value to the console.
9+
// Using a for..in loop, iterate through each property in the tortoiseOne object and print the value to the console.
10+
11+
for (item in tortoiseOne) {
12+
console.log(`${item}, ${tortoiseOne[item
13+
]}`);
14+
15+
}
16+
17+
console.log("-".repeat(40));
18+
//Lets try adding to it
19+
for (item in tortoiseOne) {
20+
console.log(`${item}, ${tortoiseOne[item] + ' This was added :-)'}`);
21+
22+
}
23+

objects-and-math/chapter-examples/KindnessSelection.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,23 @@ function randomSelection(arr){
1313

1414
//Experiment with the code above. Try to:
1515
//a) Print 3 random selections from each array.
16+
for (let i = 0; i < 3; i++) {
17+
console.log(`happiness: ${randomSelection(happiness)}`);
18+
console.log(`words: ${randomSelection(words)}`);
19+
}
1620
//b) Have the code randomly pick one array, and then print 2 random items from it.
21+
22+
let zeroOrOne = Math.floor(Math.random() * 2);
23+
console.log(`zeroOrOne: ${zeroOrOne}`);
24+
if (zeroOrOne === 0) {
25+
console.log(`Random array happiness`);
26+
console.log(`happiness: ${randomSelection(happiness)}`);
27+
console.log(`happiness: ${randomSelection(happiness)}`);
28+
29+
} else {
30+
console.log(`Random array words`);
31+
console.log(`words: ${randomSelection(words)}`);
32+
console.log(`words: ${randomSelection(words)}`);
33+
}
34+
1735
//c) Create a new array, then fill it with one random item from words and happiness. Print the new array.

objects-and-math/exercises/ObjectExercises.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,79 @@ let salamander = {
1212
age: 5
1313
};
1414

15+
let superChimpTwo = {
16+
name: "Brad",
17+
species: "Chimpanzee",
18+
mass: 11,
19+
age: 6
20+
};
21+
22+
let beagle = {
23+
name: "Leroy",
24+
species: "Beagle",
25+
mass: 14,
26+
age: 5
27+
};
28+
29+
let tardigrade = {
30+
name: "Almina",
31+
species: "Tardigrade",
32+
mass: 0.0000000001,
33+
age: 1
34+
};
35+
36+
let crew = [superChimpOne, salamander,superChimpTwo, beagle, tardigrade];
1537

1638
// After you have created the other object literals, add the astronautID property to each one.
39+
let checkForSameId = [];
40+
function addAstronautID (animals) {
41+
animals.astronautID = Math.ceil(Math.random() * 10);
42+
43+
while (checkForSameId.includes(animals.astronautID)){
44+
animals.astronautID = Math.ceil(Math.random() * 10);
45+
}
46+
checkForSameId.push(animals.astronautID);
47+
48+
return animals;
49+
}
1750

1851
// Add a move method to each animal object
52+
function addMoveMethod(animal) {
53+
animal.move = function () {
54+
let steps = Math.floor(Math.random() * 11);
55+
return steps;
56+
}
57+
return animal;
58+
}
1959

20-
// Create an array to hold the animal objects.
60+
for (let i = 0 ; i < crew.length; i++) {
61+
addAstronautID(crew[i]);
62+
addMoveMethod(crew[i]);
63+
}
64+
console.log(crew);
2165

2266
// Print out the relevant information about each animal.
67+
function crewReports (animal) {
68+
return `${animal.name} is a ${animal.species}. They are ${animal.age} years old and ${animal.mass} kilograms. Their ID is ${animal.astronautID}.`;
69+
}
70+
71+
console.log(crewReports(tardigrade));
2372

2473
// Start an animal race!
74+
function fitnessTest(arr) {
75+
let result = [];
76+
for (let i = 0; i < arr.length; i++){
77+
let turns = 0;
78+
let steps = 0;
79+
while (steps < 21) {
80+
steps += arr[i].move();
81+
turns++;
82+
}
83+
result.push(`${arr[i].name} took ${turns} turns to take 20 steps.`);
84+
}
85+
return result;
86+
}
87+
88+
fitnessTest(crew).forEach(element => {
89+
console.log(element);
90+
});

0 commit comments

Comments
 (0)