Recently, I've been working on a method that calculates the total number of freebies based on an array of items. While the current implementation gets the job done, it seems like there is a fair amount of redundant code present. I'm looking for suggestions on how to clean up and refactor this code for better efficiency. Thoughts?
freebieCalculator(orders) {
const freeItemAmount = [];
const totalAmount = [];
for (const order of orders) {
const items = Math.floor(order.cash / order.price);
const freebies = Math.floor(items / order.bonus_ratio);
if (order.type === 'foo') {
const bar = 0;
const boo = 0;
let foo = 0;
foo = (foo + 1) * freebies;
freeItemAmount.push({'\"foo\"': foo, '\"bar\"': bar, '\"boo\"' : boo});
totalAmount.push({'\"foo\"': foo + items, '\"bar\"': bar, '\"boo\"' : boo});
} else if (order.type === 'bar') {
const foo = 0;
const boo = 0;
let bar = 0;
bar = (bar + 2) * freebies;
freeItemAmount.push({'\"foo\"': foo, '\"bar\"': bar, '\"boo\"' : boo});
totalAmount.push({'\"foo\"': foo, '\"bar\"': bar + items, '\"boo\"' : boo});
} else if (order.type === 'boo') {
const bar = 0;
let foo = 0;
let boo = 0;
foo = (foo + 1) * freebies;
boo = (boo + 1) * freebies;
freeItemAmount.push({'\"foo\"': foo, '\"bar\"': bar, '\"boo\"' : boo});
totalAmount.push({'\"foo\"': foo, '\"bar\"': bar, '\"boo\"' : boo + items});
}
}
return totalAmount;
}