Udemy. 11. Arrays.

challenge #4

by trentHarlem

HTML

<h1>
///////////////////////////////////////<br>
// Coding Challenge #4
</h1>
<p>
Julia and Kate are still studying dogs, and this time they are studying if dogs are eating too much or too little.<br>
Eating too much means the dog's current food portion is larger than the recommended portion, and eating too little is the opposite.<br>
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).<br><br>

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.<br>
Forumla: recommendedFood = weight ** 0.75 * 28. (The result is in grams of food, and the weight needs to be in kg)<br>
<br><br>
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) 🤓<br><br>
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').<br><br>
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!"<br><br>
5. Log to the console whether there is any dog eating EXACTLY the amount of food that is recommended (just true or false)<br><br>
6. Log to the console whether there is any dog eating an OKAY amount of food (just true or false)<br><br>
7. Create an array containing the dogs that are eating an OKAY amount of food (try to reuse the condition used in 6.)<br><br>
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)<br><br>

HINT 1: Use many different tools to solve...

CSS

body {
  font: 1.1em system-ui;
}

JavaScript

//TEST DATA:
const dogs = [{
    weight: 22,
    curFood: 250,
    owners: ['Alice', 'Bob']
  },
  {
    weight: 8,
    curFood: 200,
    owners: ['Matilda']
  },
  {
    weight: 13,
    curFood: 275,
    owners: ['Sarah', 'John']
  },
  {
    weight: 32,
    curFood: 340,
    owners: ['Michael']
  }
];
// 1. add recommended food property to dogs object
/* dogs.forEach(dog => dog.recFood = dog.weight * 0.75 * 28); */
dogs.forEach(dog => dog.recFood = Math.floor(dog.weight ** 0.75 * 28));
console.log('dogs',dogs);

//2.   find Sarah's dog. 
//     log whether it's eating too much or too little.

//const sarahDog = dogs.find(dog => dog.owners.includes('Sarah') && dog.curFood > (dog.recFood * 0.90) && dog.curFood < (dog.recFood * 1.10))

const sarahDog = dogs.find(dog => dog.owners.includes('Sarah'))
console.log(sarahDog)

console.log(`Sarah's dog is eating too ${sarahDog.curFood>sarahDog.recFood?'much':'little'}`)



/*
const sarahDog = dogs.find(dog => {
dogs.forEach(dog => {
  if (dog.owners.includes('Sarah') && dog.curFood > (dog.recFood * 0.90) && dog.curFood < (dog.recFood * 1.10)) {
    console.log(`The dog owned by ${dog.owners[0]}, is eating fine`)
  }
}) 
console.log(sarahDog)
*/

// 3.  create arrays
// 4. log to console

/* const ownersEatTooMuch = [];
const ownersEatTooLittle = [];
dogs.map(dog => {
  if (dog.curFood > (dog.recFood * 1.10)) {
    ownersEatTooMuch.push(...dog.owners)
  }
  if (dog.curFood < (dog.recFood * 0.90)) {
    ownersEatTooLittle.push(...dog.owners)
  }
}) */

const ownersEatTooMuch = dogs.filter(dog =>dog.curFood>dog.recFood).flatMap(dog=>dog.owners)
const ownersEatTooLittle = dogs.filter(dog =>dog.curFood<dog.recFood).flatMap(dog=>dog.owners)

console.log(`${ownersEatTooMuch.join(' and ')}'s dogs eat too much!`);
console.log(`${ownersEatTooLittle.join(' and ')}'s dogs eat too little!`);

// 5. exact amount of food
// 'some' method will return true if condition is true for  ANY of the items
console.log(dogs.some(dog =>...