|
1 | | -'use-strict'; |
| 1 | +'use strict'; |
| 2 | + |
2 | 3 | /* |
3 | 4 | ? ------------Coding Challenge 1------------ |
4 | 5 |
|
@@ -99,3 +100,110 @@ function calcAverageHumanAge(ages) { |
99 | 100 | } |
100 | 101 | console.log(calcAverageHumanAge([5, 2, 4, 1, 15, 8, 3])); |
101 | 102 | console.log(calcAverageHumanAge([16, 6, 10, 5, 6, 1, 4])); |
| 103 | + |
| 104 | +/////////////////////////////////////// |
| 105 | +// Coding Challenge #4 |
| 106 | + |
| 107 | +/* |
| 108 | +Julia and Kate are still studying dogs, and this time they are studying if the dogs are eating too much or too little. |
| 109 | +Eating too much means the dog's current food portion is larger than the recommended portion, and eating too little is the opposite. |
| 110 | +Eating an okay amount means the dog's current food portion is within a range 10% above and 10% below the recommended portion (see hint). |
| 111 | +1. Loop over the array containing dog objects, and for each dog, calculate the recommended food portion and add it to the object as a new property. Do NOT create a new array, simply loop over the array. Forumla: recommendedFood = weight ** 0.75 * 28. (The result is in grams of food, and the weight needs to be in kg) |
| 112 | +2. Find Sarah's dog and log to the console whether it's eating too much or too little. HINT: Some dogs have multiple owners, so you first need to find Sarah in the owners array, and so this one is a bit tricky (on purpose) 🤓 |
| 113 | +3. Create an array containing all owners of dogs who eat too much ('ownersEatTooMuch') and an array with all owners of dogs who eat too little ('ownersEatTooLittle'). |
| 114 | +4. Log a string to the console for each array created in 3., like this: "Matilda and Alice and Bob's dogs eat too much!" and "Sarah and John and Michael's dogs eat too little!" |
| 115 | +5. Log to the console whether there is any dog eating EXACTLY the amount of food that is recommended (just true or false) |
| 116 | +6. Log to the console whether there is any dog eating an OKAY amount of food (just true or false) |
| 117 | +7. Create an array containing the dogs that are eating an OKAY amount of food (try to reuse the condition used in 6.) |
| 118 | +8. Create a shallow copy of the dogs array and sort it by recommended food portion in an ascending order (keep in mind that the portions are inside the array's objects) |
| 119 | +HINT 1: Use many different tools to solve these challenges, you can use the summary lecture to choose between them 😉 |
| 120 | +HINT 2: Being within a range 10% above and below the recommended portion means: current > (recommended * 0.90) && current < (recommended * 1.10). Basically, the current portion should be between 90% and 110% of the recommended portion. |
| 121 | +TEST DATA: |
| 122 | +const dogs = [ |
| 123 | + { weight: 22, curFood: 250, owners: ['Alice', 'Bob'] }, |
| 124 | + { weight: 8, curFood: 200, owners: ['Matilda'] }, |
| 125 | + { weight: 13, curFood: 275, owners: ['Sarah', 'John'] }, |
| 126 | + { weight: 32, curFood: 340, owners: ['Michael'] } |
| 127 | +]; |
| 128 | +GOOD LUCK 😀 |
| 129 | +*/ |
| 130 | + |
| 131 | +// const recommendedFood = weight ** 0.75 * 28; |
| 132 | + |
| 133 | +const dogs = [ |
| 134 | + { |
| 135 | + weight: 22, |
| 136 | + curFood: 250, |
| 137 | + owners: ['Alice', 'Bob'], |
| 138 | + }, |
| 139 | + { |
| 140 | + weight: 8, |
| 141 | + curFood: 200, |
| 142 | + owners: ['Matilda'], |
| 143 | + }, |
| 144 | + { |
| 145 | + weight: 13, |
| 146 | + curFood: 275, |
| 147 | + owners: ['Sarah', 'John'], |
| 148 | + }, |
| 149 | + { |
| 150 | + weight: 32, |
| 151 | + curFood: 340, |
| 152 | + owners: ['Michael'], |
| 153 | + }, |
| 154 | +]; |
| 155 | + |
| 156 | +// 1. |
| 157 | +dogs.forEach( |
| 158 | + dog => (dog.recommendedFood = Math.trunc(dog.weight ** 0.75 * 28)) |
| 159 | +); |
| 160 | + |
| 161 | +// 2. |
| 162 | +const sarahDog = dogs.find(dog => dog.owners.includes('Sarah')); |
| 163 | +console.log( |
| 164 | + `Sarah's dog is eating too ${ |
| 165 | + sarahDog.curFood > sarahDog.recommendedFood ? 'much' : 'little' |
| 166 | + }` |
| 167 | +); |
| 168 | +console.log(dogs); |
| 169 | +console.log(sarahDog); |
| 170 | + |
| 171 | +// 3. |
| 172 | +const ownersEatTooMuch = dogs |
| 173 | + .filter(dog => dog.curFood > dog.recommendedFood) |
| 174 | + .flatMap(owner => owner.owners); |
| 175 | + |
| 176 | +const ownersEatTooLittle = dogs |
| 177 | + .filter(dog => dog.curFood < dog.recommendedFood) |
| 178 | + .flatMap(owner => owner.owners); |
| 179 | + |
| 180 | +console.log(ownersEatTooMuch, ownersEatTooLittle); |
| 181 | + |
| 182 | +// 4. |
| 183 | +console.log(`${ownersEatTooMuch.join(' and ')}'s dogs eat too much!`); |
| 184 | +console.log(`${ownersEatTooLittle.join(' and ')}'s dogs eat too little!`); |
| 185 | + |
| 186 | +// 5. |
| 187 | +const checkExactFood = dogs.some(dog => dog.curFood === dog.recommendedFood); |
| 188 | +console.log(checkExactFood); |
| 189 | + |
| 190 | +// 6. |
| 191 | +const okayAmountFood = dogs.some( |
| 192 | + dog => |
| 193 | + dog.curFood > dog.recommendedFood * 0.9 && |
| 194 | + dog.curFood < dog.recommendedFood * 1.1 |
| 195 | +); |
| 196 | +console.log(okayAmountFood); |
| 197 | + |
| 198 | +// 7. |
| 199 | +const okayFoodArray = dogs.filter( |
| 200 | + dog => |
| 201 | + dog.curFood > dog.recommendedFood * 0.9 && |
| 202 | + dog.curFood < dog.recommendedFood * 1.1 |
| 203 | +); |
| 204 | +console.log(okayFoodArray); |
| 205 | + |
| 206 | +const dogsSorted = dogs |
| 207 | + .slice() |
| 208 | + .sort((a, b) => a.recommendedFood - b.recommendedFood); |
| 209 | +console.log(dogsSorted); |
0 commit comments