|
1 | 1 | // Code your selectRandomEntry function here:
|
2 | 2 |
|
| 3 | +function selectRandomEntry(arr) { |
| 4 | + return arr[Math.round(Math.random() * 5)]; |
| 5 | +} |
3 | 6 |
|
4 | 7 | // Code your buildCrewArray function here:
|
5 | 8 |
|
6 |
| - |
7 | 9 | let idNumbers = [291, 414, 503, 599, 796, 890];
|
8 | 10 |
|
| 11 | +let randomArr = []; |
| 12 | +let usedVal = []; |
| 13 | + |
| 14 | +while (randomArr.length < 3) { |
| 15 | + let val = selectRandomEntry(idNumbers); |
| 16 | + if (!usedVal.includes(val)) { |
| 17 | + randomArr.push(val); |
| 18 | + usedVal.push(val); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +console.log(randomArr); |
| 23 | + |
| 24 | +console.log(selectRandomEntry(idNumbers)); |
| 25 | + |
9 | 26 | // Here are the candidates and the 'animals' array:
|
10 | 27 | let candidateA = {
|
11 |
| - 'name':'Gordon Shumway', |
12 |
| - 'species':'alf', |
13 |
| - 'mass':90, |
14 |
| - 'o2Used':function(hrs){return 0.035*hrs}, |
15 |
| - 'astronautID':414 |
| 28 | + name: "Gordon Shumway", |
| 29 | + species: "alf", |
| 30 | + mass: 90, |
| 31 | + o2Used: function (hrs) { |
| 32 | + return 0.035 * hrs; |
| 33 | + }, |
| 34 | + astronautID: 414, |
16 | 35 | };
|
17 | 36 | let candidateB = {
|
18 |
| - 'name':'Lassie', |
19 |
| - 'species':'dog', |
20 |
| - 'mass':19.1, |
21 |
| - 'o2Used':function(hrs){return 0.030*hrs}, |
22 |
| - 'astronautID':503 |
| 37 | + name: "Lassie", |
| 38 | + species: "dog", |
| 39 | + mass: 19.1, |
| 40 | + o2Used: function (hrs) { |
| 41 | + return 0.03 * hrs; |
| 42 | + }, |
| 43 | + astronautID: 503, |
23 | 44 | };
|
24 | 45 | let candidateC = {
|
25 |
| - 'name':'Jonsey', |
26 |
| - 'species':'cat', |
27 |
| - 'mass':3.6, |
28 |
| - 'o2Used':function(hrs){return 0.022*hrs}, |
29 |
| - 'astronautID':796 |
| 46 | + name: "Jonsey", |
| 47 | + species: "cat", |
| 48 | + mass: 3.6, |
| 49 | + o2Used: function (hrs) { |
| 50 | + return 0.022 * hrs; |
| 51 | + }, |
| 52 | + astronautID: 796, |
30 | 53 | };
|
31 | 54 | let candidateD = {
|
32 |
| - 'name':'Paddington', |
33 |
| - 'species':'bear', |
34 |
| - 'mass':31.8, |
35 |
| - 'o2Used':function(hrs){return 0.047*hrs}, |
36 |
| - 'astronautID':291 |
| 55 | + name: "Paddington", |
| 56 | + species: "bear", |
| 57 | + mass: 31.8, |
| 58 | + o2Used: function (hrs) { |
| 59 | + return 0.047 * hrs; |
| 60 | + }, |
| 61 | + astronautID: 291, |
37 | 62 | };
|
38 | 63 | let candidateE = {
|
39 |
| - 'name':'Pete', |
40 |
| - 'species':'tortoise', |
41 |
| - 'mass':417, |
42 |
| - 'o2Used':function(hrs){return 0.010*hrs}, |
43 |
| - 'astronautID':599 |
| 64 | + name: "Pete", |
| 65 | + species: "tortoise", |
| 66 | + mass: 417, |
| 67 | + o2Used: function (hrs) { |
| 68 | + return 0.01 * hrs; |
| 69 | + }, |
| 70 | + astronautID: 599, |
44 | 71 | };
|
45 | 72 | let candidateF = {
|
46 |
| - 'name':'Hugs', |
47 |
| - 'species':'ball python', |
48 |
| - 'mass':2.3, |
49 |
| - 'o2Used':function(hrs){return 0.018*hrs}, |
50 |
| - 'astronautID':890 |
| 73 | + name: "Hugs", |
| 74 | + species: "ball python", |
| 75 | + mass: 2.3, |
| 76 | + o2Used: function (hrs) { |
| 77 | + return 0.018 * hrs; |
| 78 | + }, |
| 79 | + astronautID: 890, |
51 | 80 | };
|
52 | 81 |
|
53 |
| -let animals = [candidateA,candidateB,candidateC,candidateD,candidateE,candidateF]; |
| 82 | +let animals = [candidateA, candidateB, candidateC, candidateD, candidateE, candidateF]; |
54 | 83 |
|
55 | 84 | // Code your template literal and console.log statements:
|
| 85 | + |
| 86 | +function buildCrewArray(numbers, animals) { |
| 87 | + let crew = []; |
| 88 | + while (crew.length < 3) { |
| 89 | + for (i = 0; i < animals.length; i++) { |
| 90 | + if (numbers.includes(animals[i].astronautID)) { |
| 91 | + crew.push(animals[i].name); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + console.log(`${crew[0]}, ${crew[1]} & ${crew[2]} are going to space!`); |
| 96 | +} |
| 97 | + |
| 98 | +buildCrewArray(randomArr, animals); |
0 commit comments