JSFiddle - React, Tailwind, and code Playground
by andrei
CSS
body {
white-space: pre
}
JavaScript
var MONEY = 35 * 100, // in cents
firstTotal = 10,
secondTotal = 16,
thirdTotal = 26,
answer = [];
var getAllCombinationsForAFarmer = function(total, priceBeforeLunch, priceAfterLunch) {
var soldBeforeLunch = 0,
combinations = {};
while (soldBeforeLunch <= total) {
var madeDuringaDay = soldBeforeLunch * priceBeforeLunch + (total - soldBeforeLunch) * priceAfterLunch;
if (madeDuringaDay == MONEY) {
combinations[priceBeforeLunch] = priceAfterLunch;
}
soldBeforeLunch++;
}
return combinations;
};
for (var priceBeforeLunch = 0; priceBeforeLunch <= MONEY; priceBeforeLunch++) {
for (var priceAfterLunch = 0; priceAfterLunch < priceBeforeLunch; priceAfterLunch++) {
var firstCombos = getAllCombinationsForAFarmer(firstTotal, priceBeforeLunch, priceAfterLunch),
secondCombos = getAllCombinationsForAFarmer(secondTotal, priceBeforeLunch, priceAfterLunch),
thirdCombos = getAllCombinationsForAFarmer(thirdTotal, priceBeforeLunch, priceAfterLunch);
if (Object.keys(firstCombos).length && Object.keys(secondCombos).length && Object.keys(thirdCombos).length) {
// now try to find all possible matches from all of combos
Object.keys(firstCombos).forEach(function (key) {
var after1 = firstCombos[key],
after2 = secondCombos[key],
after3 = thirdCombos[key];
if (after1 == after2 && after2 == after3) {
answer.push({
before: priceBeforeLunch,
after: priceAfterLunch
});
}
});
}
}
}
document.querySelectorAll("body")[0].innerHTML = "FOUND ANSWER: <br/> "+ JSON.stringify(answer, null, 4);