#What is the fastest way to show total price?

6 messages · Page 1 of 1 (latest)

peak garnet
#

I am doing this: function getOrderHtml() {

let orderHtml = ``
orderArray.forEach(function(order){
orderHtml += `
    <div>
        <p class="food-name">${order.name}</p>
        <p class="food-name">${order.ingredients}</p>
        <p class="food-name">${order.price}</p>
    </div>`

})

to get the listed items which I choose from the menu. Now I want to show the total price under it of all the products. How can and how should I calculate that? And should I use the same function, or a new one?

here my scrim: https://scrimba.com/scrim/co060441c952162626eade4ed

Scrimba

Learn to code with interactive screencasts. Our courses and tutorials will teach you React, Vue, Angular, JavaScript, HTML, CSS, and more. Scrimba is the fun and easy way to learn web development.

midnight widget
#

First you declare a variable that will store the total price value (totalPrice). With every iteration of the forEach loop, you add the price of an item you added to the order to that variable.

peak garnet
#

I love you, thank you so much !!! 😄

dreamy peak
#

As a learning exercise this is the perfect use of the reduce() method... are you familiar with reduce()?

If not, reduce is used to iterate over a list of items and "reduce" that list to a single result... hmmm... that sounds a LOT like what you're trying to do here.

I agree with @midnight widget that you're "already here" looping over the orders array but give reduce a try just for the sake of learning it.

midnight widget