Advertisement
Guest User

Untitled

a guest
Jan 6th, 2022
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function foodForPets(input) {
  2.  
  3.     let index = 0;
  4.     let days = Number(input[index]);
  5.     index++;
  6.     let totalFood = Number(input[index]);
  7.     index++;
  8.     let foodEatenByDog = 0;
  9.     let foodEatenByCat = 0;
  10.     let sumFoodEatenByDog = 0;
  11.     let sumFoodEatenByCat = 0;
  12.     let bisquits = 0;
  13.  
  14.     for (let i = 1; i <= days; i++) {
  15.         foodEatenByDog = Number(input[index]);
  16.         sumFoodEatenByDog += foodEatenByDog;
  17.         index++;
  18.         foodEatenByCat = Number(input[index]);
  19.         sumFoodEatenByCat += foodEatenByCat;
  20.         index++;
  21.         if (i % 3 == 0) {
  22.             bisquits = bisquits + ((foodEatenByCat + foodEatenByDog) * 0.10);
  23.         }
  24.     }
  25.  
  26.     let totalEatenFood = sumFoodEatenByDog + sumFoodEatenByCat;
  27.  
  28.     let percentTotalEatenFood = (totalEatenFood / totalFood) * 100;
  29.     let percentTotalEatenFoodByDog = (sumFoodEatenByDog / totalEatenFood) * 100;
  30.     let percentTotalEatenFoodByCat = (sumFoodEatenByCat / totalEatenFood) * 100;
  31.  
  32.     console.log(`Total eaten biscuits: ${Math.round(bisquits)}gr.`);
  33.     console.log(`${percentTotalEatenFood.toFixed(2)}% of the food has been eaten.`);
  34.     console.log(`${percentTotalEatenFoodByDog.toFixed(2)}% eaten from the dog.`);
  35.     console.log(`${percentTotalEatenFoodByCat.toFixed(2)}% eaten from the cat.`);
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement