(JavaScript) Решение на зад. 4 Храна за домашни любимци от Изпит - 28 и 29 март 2020
Здравейте,
Някой може ли да предложи решение на зад. 4 Храна за домашни любимци от Изпита от 28 и 29 март 2020?
Благодаря.
Здравейте,
Някой може ли да предложи решение на зад. 4 Храна за домашни любимци от Изпита от 28 и 29 март 2020?
Благодаря.
Two solutions from other collegues (you can find them with the search button using "Food for Pets"):
function footFotPets(input) {
let days = Number(input[0]);
let totalFoot = Number(input[1]);
let sumDogFoot = 0;
let sumCatFoot = 0;
let totalEatenFoot = 0;
let totalPercent = 0;
let sumBisscuits = 0;
let totalDogPercents = 0;
let totalCatPercents = 0;
let counter = 2;
for (let i = 1; i <= days; i++) {
let dogFoot = Number(input[counter++]);
let catFoot = Number(input[counter++]);
sumDogFoot += dogFoot;
sumCatFoot += catFoot;
if (i % 3 == 0) {
let currentSumBisscuits = (dogFoot + catFoot) * 0.10;
sumBisscuits += currentSumBisscuits;
}
}
totalEatenFoot = sumDogFoot + sumCatFoot;
totalDogPercents = (sumDogFoot / totalEatenFoot) * 100;
totalCatPercents = (sumCatFoot / totalEatenFoot) * 100;
totalPercent = (totalEatenFoot / totalFoot) * 100;
console.log(`Total eaten biscuits: ${Math.round(sumBisscuits)}gr.`);
console.log(`${totalPercent.toFixed(2)}% of the food has been eaten.`);
console.log(`${totalDogPercents.toFixed(2)}% eaten from the dog.`);
}
function petFood(input) {
let days = Number(input.shift());
let totalFood = Number(input.shift());
let dogFood = 0;
let catFood = 0;
let counter = 0;
let bonus = 0;
for (let i = 1; i <= days; i++) {
let Dogsfood = Number(input.shift());
dogFood += Dogsfood;
let Catsfood = Number(input.shift());
catFood += Catsfood;
counter++;
if (counter % 3 ===0) {
bonus += (Dogsfood + Catsfood) * 0.1;
}
}
bonus = Math.round(bonus);
console.log(`Total eaten biscuits: ${bonus}gr.`);
console.log(`${(((dogFood + catFood) / totalFood) * 100).toFixed(2)}% of the food has been eaten.`);
console.log(`${((dogFood / (dogFood + catFood)) * 100).toFixed(2)}% eaten from the dog.`);
console.log(`${((catFood / (dogFood + catFood)) * 100).toFixed(2)}% eaten from the cat.`);
}