Advertisement
Liliana797979

food for pets with for loop

Jan 3rd, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function foodForPets(input) {
  3.     let index = 0;
  4.     let days = Number(input[index]);
  5.     index++;
  6.     let totalFood = Number(input[index]);
  7.     index++;
  8.  
  9.     let dogFood = 0;
  10.     let catFood = 0;
  11.     let biscuitsDog = 0;
  12.     let biscuitsCat = 0;
  13.     let biscuitsTotal = 0;
  14.     let eatenFood = 0;
  15.  
  16.     for (let i = 1; i <= days; i++) {
  17.         if (i % 3 === 0) {
  18.             biscuitsDog = Number(input[index]) * 0.10;
  19.             biscuitsTotal += biscuitsDog;
  20.         }
  21.         dogFood += Number(input[index]);
  22.         index++;
  23.         if (i % 3 === 0) {
  24.             biscuitsCat = Number(input[index]) * 0.10;
  25.             biscuitsTotal += biscuitsCat;
  26.         }
  27.         catFood += Number(input[index]);
  28.         index++;
  29.     }
  30.  
  31.     eatenFood = dogFood + catFood;
  32.  
  33.     let percentageEatenFood = eatenFood / totalFood * 100;
  34.     let percentageEatenFoodByDog = dogFood / eatenFood * 100;
  35.     let percentageEatenFoodByCat = catFood / eatenFood * 100;
  36.  
  37.     console.log(`Total eaten biscuits: ${Math.round(biscuitsTotal)}gr.`);
  38.     console.log(`${percentageEatenFood.toFixed(2)}% of the food has been eaten.`);
  39.     console.log(`${percentageEatenFoodByDog.toFixed(2)}% eaten from the dog.`);
  40.     console.log(`${percentageEatenFoodByCat.toFixed(2)}% eaten from the cat.`);
  41. }
  42.  
  43. foodForPets(['3', '1000', '300', '20', '100', '30', '110', '40']);
  44. foodForPets(['3', '500', '100', '30', '110', '25', '120', '35']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement