-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
animalFarm.js
29 lines (19 loc) · 988 Bytes
/
animalFarm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
Assuming there are no other types of animals, work out how many of each animal are there.
Return a tuple in Python - (chickens, cows) and an array list - [chickens, cows]/{chickens, cows} in all other languages
If either the heads & legs is negative, the result of your calculation is negative or the calculation is a float return "No solutions" (no valid cases).
In the form:
[Heads, Legs] = [72, 200]
VALID - [72, 200] => [44 , 28]
[Chickens, Cows]
INVALID - [72, 201] => "No solutions"
However, if 0 heads and 0 legs are given always return [0, 0] since zero heads must give zero animals.
There are many different ways to solve this, but they all give the same answer.
You will only be given integers types - however negative values (edge cases) will be given.
*/
//Answer//
function animals(h, l){
let chicken = h-((l-(2*h))/2)
let cow = (l-(2*h))/2
return !((cow*chicken)%1)&&cow>=0&&chicken>=0?[chicken,cow]:'No solutions'
}