var divideSteps = [
{
step: 1500,
price: 2,
},
{
step: 200,
price: 3,
},
{
step: 500,
price: 4,
}
];
var lastStepIndex = divideSteps.length - 1;
var totalItems = 4000;
var fullPrice = 0;
divideSteps.forEach((x,i) => {
if (i === lastStepIndex) {
fullPrice += totalItems * x.price;
} else if (x.step > totalItems) {
fullPrice += x.step * x.price;
totalItems -= x.step;
} else {
fullPrice += totalItems * x.price;
totalItems = 0;
};
});
Заметьте, что конструкция switch/case использует нестрогое сравнение (==).