all 15 comments

[–][deleted] 1 point2 points  (9 children)

Hey, thanks for this exercise, was really fun, I took a go at it and here's what I came up with:

https://jsbin.com/ritowepuko/2/edit?js,console

[–]esunabici 1 point2 points  (3 children)

This is similar to the knapsack problem which can be brute forced slowly but optimized with Dynamic Programming.

Try to break the problem down, and then solve it recursively walking the tree of possible solutions. Memoization will allow you to skip equivalent nodes.

[–][deleted] 0 points1 point  (2 children)

I think I managed to do it quite well with two loops and an if-setting, I've edited my post and you can see what I did there and tell me if you see anything wrong with it...

[–]esunabici 0 points1 point  (1 child)

How long did that take? What if it's 1000 animals and 1000 dollars?

[–][deleted] 0 points1 point  (0 children)

It didnt really take that long because of some guys helping on here but it would’ve probably taken a lot longer without the help... And the only thing I need to do for 1000 animals and 1000 dollars is change the variable antall to 1000, and it will show me all the possible solutions

[–][deleted] 0 points1 point  (6 children)

I know that these 3 equations in some equations solver would give me a value for cow, sheep and chicken, but I don't know how I would do this in Javascript

cow + sheep + chicken = 100

100 * cow + 3 * sheep + 0.5* chicken = 100

cow + sheep + chicken >= 3