The function accepts two positive integers ‘r’ and ‘unit’ and a positive integer array ‘arr’ of size ‘n’ as its argument ‘r’ represents the number of rats present in an area, ‘unit’ is the amount of food each rat consumes and each ith element of array ‘arr’ represents the amount of food present in ‘i+1’ house number, where 0 <= i
function func(r, unit, arr) {
const needFood = r * unit
const suppliesFood = arr.map(i => x += i, x = 0).reverse()[0]
if (needFood <= suppliesFood) {
return 'Enough food for the rats'
} else {
return "The rats don't have enough food"
}
}
console.log(func(10, 5, [20, 16, 5, 10, 12, 3]))
Comments
Leave a comment