@@ -92,7 +92,7 @@ calcAverageHumanAge(dogsAge1);
9292console . log ( '----- TEST 2 -----' ) ;
9393calcAverageHumanAge ( dogsAge2 ) ;
9494
95- // Coding Challenge 2
95+ // Coding Challenge 3
9696
9797/*
9898Rewrite the 'calcAverageHumanAge' function from Challenge #2, but this time
@@ -115,4 +115,112 @@ console.log('----- TEST 1 REV -----');
115115calcAverageHumanAgeRev ( dogsAge1 ) ;
116116
117117console . log ( '----- TEST 2 REV -----' ) ;
118- calcAverageHumanAgeRev ( dogsAge2 ) ;
118+ calcAverageHumanAgeRev ( dogsAge2 ) ;
119+
120+ // Coding Challenge 4
121+
122+ /*
123+ Julia and Kate are still studying dogs, and this time they are studying if dogs are
124+ eating too much or too little.
125+ Eating too much means the dog's current food portion is larger than the
126+ recommended portion, and eating too little is the opposite.
127+ Eating an okay amount means the dog's current food portion is within a range 10%
128+ above and 10% below the recommended portion (see hint).
129+ Your tasks:
130+ 1. Loop over the 'dogs' array containing dog objects, and for each dog, calculate
131+ the recommended food portion and add it to the object as a new property. Do
132+ not create a new array, simply loop over the array. Forumla:
133+ recommendedFood = weight ** 0.75 * 28. (The result is in grams of
134+ food, and the weight needs to be in kg)
135+ 2. Find Sarah's dog and log to the console whether it's eating too much or too
136+ little. Hint: Some dogs have multiple owners, so you first need to find Sarah in
137+ the owners array, and so this one is a bit tricky (on purpose) 🤓
138+ 3. Create an array containing all owners of dogs who eat too much
139+ ('ownersEatTooMuch') and an array with all owners of dogs who eat too little
140+ ('ownersEatTooLittle').
141+ 4. Log a string to the console for each array created in 3., like this: "Matilda and
142+ Alice and Bob's dogs eat too much!" and "Sarah and John and Michael's dogs eat
143+ too little!"
144+ 5. Log to the console whether there is any dog eating exactly the amount of food
145+ that is recommended (just true or false)
146+ 6. Log to the console whether there is any dog eating an okay amount of food
147+ (just true or false)
148+ 7. Create an array containing the dogs that are eating an okay amount of food (try
149+ to reuse the condition used in 6.)
150+ 8. Create a shallow copy of the 'dogs' array and sort it by recommended food
151+ portion in an ascending order (keep in mind that the portions are inside the
152+ array's objects 😉)
153+ The Complete JavaScript Course 26
154+ Hints:
155+ § Use many different tools to solve these challenges, you can use the summary
156+ lecture to choose between them 😉
157+ § Being within a range 10% above and below the recommended portion means:
158+ current > (recommended * 0.90) && current < (recommended *
159+ 1.10). Basically, the current portion should be between 90% and 110% of the
160+ recommended portion.
161+ GOOD LUCK 😀
162+ */
163+
164+ const dogs = [
165+ { weight : 22 , curFood : 250 , owners : [ 'Alice' , 'Bob' ] } ,
166+ { weight : 8 , curFood : 200 , owners : [ 'Matilda' ] } ,
167+ { weight : 13 , curFood : 275 , owners : [ 'Sarah' , 'John' ] } ,
168+ { weight : 32 , curFood : 340 , owners : [ 'Michael' ] } ,
169+ ] ;
170+
171+ dogs . forEach ( dog => dog . recFood = dog . weight ** 0.75 * 28 ) ;
172+ console . log ( dogs ) ;
173+
174+ const tooLittle = - 1 ;
175+ const normal = 0 ;
176+ const tooMuch = 1 ;
177+
178+ const checkEating = dog =>
179+ {
180+ if ( dog . curFood > dog . recFood * 0.9 && dog . curFood < dog . recFood * 1.1 )
181+ return normal ;
182+ else
183+ return ( dog . curFood <= dog . recFood * 0.9 ) ? tooLittle : tooMuch ;
184+ } ;
185+
186+ dogOwners = dogs . map ( dog => dog . owners ) ;
187+ const sarahDog = dogOwners . find ( owner => owner . includes ( 'Sarah' ) ) ;
188+
189+ const check = checkEating ( sarahDog ) ;
190+ let msg = '' ;
191+
192+ if ( check === 0 )
193+ msg = 'normal' ;
194+ else
195+ msg = ( check === tooLittle ) ? 'too Little' : 'too Much' ;
196+
197+ console . log ( `Sarah's Dog is Eating ${ msg } ` ) ; ;
198+
199+ const dogOwnersEatTooMuch = [ ] ;
200+ const dogOwnersEatTooLittle = [ ] ;
201+ const dogOwnersEatOkay = [ ] ;
202+
203+ dogs . forEach ( dog =>
204+ {
205+ const check = checkEating ( dog ) ;
206+
207+ if ( check === tooLittle )
208+ dogOwnersEatTooLittle . push ( dog . owners ) ;
209+ else if ( check === tooMuch )
210+ dogOwnersEatTooMuch . push ( dog . owners ) ;
211+ else
212+ dogOwnersEatOkay . push ( dog . owners ) ;
213+ } ) ;
214+ console . log ( `Dogs that Eat too Much: ${ dogOwnersEatTooMuch } \n\
215+ Dogs that Eat too Little: ${ dogOwnersEatTooLittle } \n\
216+ Dogs that Eat an Okay Amount: ${ dogOwnersEatOkay } ` ) ;
217+
218+ console . log ( `${ dogOwnersEatTooMuch . flat ( ) . join ( ' and ' ) } 's dogs eat too much!` ) ;
219+ console . log ( `${ dogOwnersEatTooLittle . flat ( ) . join ( ' and ' ) } 's dogs eat too little!` ) ;
220+
221+ console . log ( `Is there any Dog Eating Exactly the Recommended Amount? ${ dogs . find ( dog => dog . curFood == dog . recFood ) ? 'Yes' : 'No' } ` ) ;
222+ console . log ( `Is there any Dog Eating an Okay Amount? ${ dogOwnersEatOkay . length > 0 ? 'Yes' : 'No' } ` ) ;
223+
224+ const dogsSorted = dogs . slice ( ) . sort ( ( a , b ) => a . recFood - b . recFood ) ;
225+ console . log ( `Dogs Sorted by Recommended Food Amount (below)` ) ;
226+ console . log ( dogsSorted ) ;
0 commit comments