Этот код вычисляет общую стоимость списка предметов, но как я могу его улучшить?

      function calculateTotal(items) {
  let total = 0;
  for (let i = 0; i < items.length; i++) {
    if (items[i].type === "food") {
      total += items[i].price * 1.1;
    } else {
      total += items[i].price;
    }
  }
  return total;
}

const items = [
  { type: "food", price: 10 },
  { type: "clothing", price: 20 },
  { type: "electronics", price: 30 }
];

console.log(calculateTotal(items));

Мне нужно улучшить этот код.

Я попытался улучшить описание переменной. вот код снова с улучшенными переменными

      function calculateTotal(items) {
  let total = 0;
  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    if (item.type === "food") {
      total += item.price * 1.1;
    } else {
      total += item.price;
    }
  }
  return total;
}

const items = [
  { type: "food", price: 10 },
  { type: "clothing", price: 20 },
  { type: "electronics", price: 30 }
];

console.log(calculateTotal(items));

что еще я могу попробовать улучшить в этом коде?

0 ответов

Другие вопросы по тегам