Skip to content

Commit 91ee9f6

Browse files
Objects and Math exercises and examples
1 parent 737ecde commit 91ee9f6

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ 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+
for (item in tortoiseOne) {
11+
console.log(item + ": " + tortoiseOne[item]);
12+
}

objects-and-math/exercises/ObjectExercises.js

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,70 @@ 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+
};
1527

16-
// After you have created the other object literals, add the astronautID property to each one.
17-
18-
// Add a move method to each animal object
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+
};
1936

20-
// Create an array to hold the animal objects.
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+
};
2145

22-
// Print out the relevant information about each animal.
46+
let crew = [superChimpOne, superChimpTwo, salamander, dog, waterBear];
2347

48+
console.log(`${superChimpOne.name} is a ${superChimpOne.species}. They are ${superChimpOne.age} years old and ${superChimpOne.mass} kilograms.`);
49+
console.log(`${superChimpTwo.name} is a ${superChimpTwo.species}. They are ${superChimpTwo.age} years old and ${superChimpTwo.mass} kilograms.`);
50+
console.log(`${salamander.name} is a ${salamander.species}. They are ${salamander.age} years old and ${salamander.mass} kilograms.`);
51+
console.log(`${dog.name} is a ${dog.species}. They are ${dog.age} years old and ${dog.mass} kilograms.`);
52+
console.log(`${waterBear.name} is a ${waterBear.species}. They are ${waterBear.age} years old and ${waterBear.mass} kilograms.`);
53+
console.log("\n");
2454
// Start an animal race!
55+
56+
57+
function fitnessTest(testingAstronauts){
58+
let results = [], numSteps, turns;
59+
for (let i = 0; i < testingAstronauts.length; i++){
60+
numSteps = 0;
61+
turns = 0;
62+
while(numSteps < 20){
63+
numSteps += testingAstronauts[i].move();
64+
turns++;
65+
}
66+
results.push(`${testingAstronauts[i].name} took ${turns} turns to take 20 steps.`);
67+
}
68+
return results;
69+
}
70+
71+
console.log(fitnessTest(crew));

0 commit comments

Comments
 (0)