I have a number of ingredients and must know the maximum number of sandwiches (Big and small sandwiches) I can make with it. If ingredients remain, the result must be false.
My code works if all ingredients can be used with only small sandwiches or maximum one big sandwich. It also returns false if ingredients are remaining. However, it returns false if we need more than one big sandwich in order to use all ingredients.
var result = []
var ispossible = function(tomatoes, cheese) {
/* we need in any case an even number of tomatoes slices */
if (tomatoes % 2 === 0) {
/* the easiest way is making as many small sandwiches as possible. The minimum number of big sandwiches is the number of cheese slices remaining when we devise the total number by 2 */
var bigSandwich = cheese % 2
var smallSandwichTomatoes = (tomatoes - 4 * bigSandwich) / 2
var smallSandwich = cheese - bigSandwich
console.log("we need" + smallSandwich + "small sandwiches and " + bigSandwich + "big sandwiches")
} else {
console.log("false, all the ingredients cannot be used")
}
}
ispossible(10, 3)
Assuming that this solution helps other readers with enhancing their rudimentary algorithm, programming skills:
const bigSmallSandwiches = (t = 10, c = 5) => (
t % 2 !== 0 || t / c !== 2 ?
'false, all the ingredients cannot be used' :
'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);
Explanation
Code-snippet:
const bigSmallSandwiches = (t = 10, c = 5) => (
t % 2 !== 0 || t / c !== 2 ?
'false, all the ingredients cannot be used' :
'we need ' + Math.floor((t - Math.floor(t / 4) * 4) / 2).toString() + ' small sandwiches and ' + Math.floor(t / 4).toString() + ' big sandwiches'
);
[
[20, 10],
[8, 4],
[4, 3],
[14, 7]
].forEach(x => console.log('tomatoes: ' + x[0] + '\tcheese slices ' + x[1] + '\n' + bigSmallSandwiches(x[0], x[1])));