Care of puppy с - class constructor - проблеми
Здравейте, опитвам се да реша тази задача с class constructor но ми дава като резултат нула, вместо правилното число, където и да местя "diff" не се получава, някой може ли да ми помогне ?
Благодаря :)
задача: https://judge.softuni.org/Contests/Practice/Index/2275#8
код:
function solve(input){
let foodBoughtGrams = Number(input.shift()) * 1000;
let diff = 0;
class Food {
constructor(totalFoodEaten){
this.totalFoodEaten = totalFoodEaten
}
}
total = () => {
this.totalFoodEaten = input.map(Number).reduce((a, v) => a + v)
diff = Math.abs(foodBoughtGrams - this.totalFoodEaten)
}
Adopted = () => input = input.filter(x => x !== "Adopted")
let MyFood = new Food()
input.forEach(x => { MyFood[x]})
this.totalFoodEaten > foodBoughtGrams ?
console.log(`Food is not enough. You need ${diff} grams more.`)
:console.log(`Food is enough! Leftovers: ${diff} grams.`)
}
solve([4, 130, 345, 400, 180, 230, 120, "Adopted"])
solve([2, 999, 456, 999, 999, 123, 456, "Adopted"])
great ! thanks a lot :)