-1

I want to get the size of each food in the menu and add the size of all of them together and display the total size of the menu in the console. Please help. My code does not work.

The desired result => total size : 225

let menu = [
    { id: 1, name: "Soda", price: 3.12, size: "4oz", type: "Drink" },
    { id: 2, name: "Beer", price: 6.50, size: "8oz", type: "Drink" },
    { id: 3, name: "Margarita", price: 12.99, size: "12oz", type: "Drink" },
    { id: 4, name: "Pizza", price: 25.10, size: "60oz", type: "Food" },
    { id: 5, name: "Kebab", price: 31.48, size: "42oz", type: "Food" },
    { id: 6, name: "Berger", price: 23.83, size: "99oz", type: "Food" },
];

const totalSize = () => {
    const b = parseFloat(menu.size);
    const result = b.forEach(itemA, itemB => itemA + itemB);
    console.log(`total  size : ${result}`)
}
totalSize();
AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
Amir
  • 63
  • 5

3 Answers3

1

menu is an array, it has no size property. You need to access the size property of each element.

You need to use reduce to combine operations between elements, not forEach.

When an arrow function takes multiple arguments, they have to be enclosed in parentheses.

let menu = [
    { id: 1, name: "Soda", price: 3.12, size: "4oz", type: "Drink" },
    { id: 2, name: "Beer", price: 6.50, size: "8oz", type: "Drink" },
    { id: 3, name: "Margarita", price: 12.99, size: "12oz", type: "Drink" },
    { id: 4, name: "Pizza", price: 25.10, size: "60oz", type: "Food" },
    { id: 5, name: "Kebab", price: 31.48, size: "42oz", type: "Food" },
    { id: 6, name: "Berger", price: 23.83, size: "99oz", type: "Food" },
];

const totalSize = () => {
    const result = menu.map(item => parseFloat(item.size)).reduce((total, item) => total + item);
    console.log(`total  size : ${result}`)
}
totalSize();
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You can use Array.prototype.reduce.

let menu = [
    { id: 1, name: "Soda", price: 3.12, size: "4oz", type: "Drink" },
    { id: 2, name: "Beer", price: 6.50, size: "8oz", type: "Drink" },
    { id: 3, name: "Margarita", price: 12.99, size: "12oz", type: "Drink" },
    { id: 4, name: "Pizza", price: 25.10, size: "60oz", type: "Food" },
    { id: 5, name: "Kebab", price: 31.48, size: "42oz", type: "Food" },
    { id: 6, name: "Berger", price: 23.83, size: "99oz", type: "Food" },
];

const totalSize = () => {
    const result = menu.reduce((sum, item) => sum + parseFloat(item.size), 0);
    console.log(`total  size : ${result}`)
}
totalSize();
Ever Dev
  • 1,882
  • 2
  • 14
  • 34
1

let menu = [
    { id: 1, name: 'Soda', price: 3.12, size: '4oz', type: 'Drink' },
    { id: 2, name: 'Beer', price: 6.5, size: '8oz', type: 'Drink' },
    { id: 3, name: 'Margarita', price: 12.99, size: '12oz', type: 'Drink' },
    { id: 4, name: 'Pizza', price: 25.1, size: '60oz', type: 'Food' },
    { id: 5, name: 'Kebab', price: 31.48, size: '42oz', type: 'Food' },
    { id: 6, name: 'Berger', price: 23.83, size: '99oz', type: 'Food' }
];

const totalSize = () => {
    let result = menu.reduce((acc, item) => {
        acc += Number(item.size.slice(0, -2));

        return acc;
    }, 0);
    console.log(`total  size : ${result}`);
};
totalSize();
Sedki Sghairi
  • 359
  • 2
  • 7