At our space base, it is a historic day! Five non-human animals are ready to run a space mission without our assistance! For the exercises, you will use the same five animal objects throughout.
Based on the two object literals provided in the starter code, create new object literals for three more animals:
Name | Species | Mass (kg) | Age (years) |
---|---|---|---|
Brad | Chimpanzee | 11 | 6 |
Leroy | Beagle | 14 | 5 |
Almina | Tardigrade | 0.0000000001 | 1 |
For each animal, add a property called astronautID
. Each astronautID
should be assigned a number between 1 and 10 (including 10). However, no
crew members should have the same ID.
Add a move
method to each animal object. The move
method will select a
random number of steps from 0 to 10 for the animal to take. The number can
be 0 as well as 10.
Create a crew
array to store all of the animal objects.
1let superChimpOne = {
2 name: "Chad",
3 species: "Chimpanzee",
4 mass: 9,
5 age: 6,
6 astronautID: 1,
7 move: function () {return Math.floor(Math.random()*11)}
8};
9
10let salamander = {
11 name: "Lacey",
12 species: "Axolotl Salamander",
13 mass: 0.1,
14 age: 5,
15 astronautID: 2,
16 move: function () {return Math.floor(Math.random()*11)}
17};
18
19let 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
28let 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
37let 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
46let crew = [superChimpOne, superChimpTwo, salamander, dog, waterBear];
47
48
49let crew = [superChimpOne, superChimpTwo, salamander, dog, waterBear];
Before these animal astronauts can get ready for launch, they need to take a
physical fitness test. Define a fitnessTest
function that takes an array as
a parameter.
Within the function, race the five animals together by using the move
method. An animal is done with the race when they reach 20 steps or greater.
Store the result as a string: '____ took ____ turns to take 20 steps.'
Fill in the blanks with the animal’s name and race result. Create a new array
to store how many turns it takes each animal to complete the race.
Return the array from the function, then print the results to the console (one animal per line).
HINT: There are a lot of different ways to approach this problem. One way
that works well is to see how many iterations of the move
method it will
take for each animal to reach 20 steps.
1function fitnessTest(candidates){
2 let results = [], numSteps, turns;
3 for (let i = 0; i < candidates.length; i++){
4 numSteps = 0;
5 turns = 0;
6 while(numSteps < 20){
7 numSteps += candidates[i].move();
8 turns++;
9 }
10 results.push(`${candidates[i].name} took ${turns} turns to take 20 steps.`);
11 }
12 return results;
13}