Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function foodForPets(input) {
- let days = Number(input[0]);
- let food = Number(input[1]);
- let dogsFood = 0;
- let catsFood = 0;
- let cookies = 0;
- let dayCount = 1;
- for (let i = 2; i <= days * 2; i += 2) {
- let dayFoodForDog = Number(input[i]);
- let dayFoodForCat = Number(input[i + 1]);
- if (dayCount % 3 === 0) {
- cookies += (dayFoodForDog + dayFoodForCat) / 10;
- }
- dogsFood += dayFoodForDog;
- catsFood += dayFoodForCat;
- dayCount++;
- }
- let totalFood = dogsFood + catsFood;
- console.log(`Total eaten biscuits: ${Math.round(cookies)}gr.`);
- console.log(`${(totalFood / food * 100).toFixed(2)}% of the food has been eaten.`);
- console.log(`${(dogsFood / totalFood * 100).toFixed(2)}% eaten from the dog.`);
- console.log(`${(catsFood / totalFood * 100).toFixed(2)}% eaten from the cat.`);
- }
- РЕШЕНИЕ СЪС SHIFT():
- function foodForPets(input) {
- let days = Number(input.shift());
- let food = Number(input.shift());
- let dogsFood = 0;
- let catsFood = 0;
- let cookies = 0;
- for (let i = 1; i <= days; i++) {
- let dayFoodForDog = Number(input.shift());
- let dayFoodForCat = Number(input.shift());
- if (i % 3 === 0) {
- cookies += (dayFoodForDog + dayFoodForCat) / 10;
- }
- dogsFood += dayFoodForDog;
- catsFood += dayFoodForCat;
- }
- let totalFood = dogsFood + catsFood;
- console.log(`Total eaten biscuits: ${Math.round(cookies)}gr.`);
- console.log(`${(totalFood / food * 100).toFixed(2)}% of the food has been eaten.`);
- console.log(`${(dogsFood / totalFood * 100).toFixed(2)}% eaten from the dog.`);
- console.log(`${(catsFood / totalFood * 100).toFixed(2)}% eaten from the cat.`);
- }
- РЕШЕНИЕ С WHILE И SHIFT():
- function foodForPets(input) {
- let days = Number(input.shift());
- let food = Number(input.shift());
- let dogsFood = 0;
- let catsFood = 0;
- let cookies = 0;
- let dayCount = 1;
- while (days-- !== 0) {
- let dayFoodForDog = Number(input.shift());
- let dayFoodForCat = Number(input.shift());
- if (dayCount++ % 3 === 0) {
- cookies += (dayFoodForDog + dayFoodForCat) / 10;
- }
- dogsFood += dayFoodForDog;
- catsFood += dayFoodForCat;
- }
- let totalFood = dogsFood + catsFood;
- console.log(`Total eaten biscuits: ${Math.round(cookies)}gr.`);
- console.log(`${(totalFood / food * 100).toFixed(2)}% of the food has been eaten.`);
- console.log(`${(dogsFood / totalFood * 100).toFixed(2)}% eaten from the dog.`);
- console.log(`${(catsFood / totalFood * 100).toFixed(2)}% eaten from the cat.`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement