Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
How do I calculate the total of multiple items?

I am trying to get a total of price of all the items multiplied by the quantity of the item:

const shoppingCart = {
    "tax": .08,
    "items": [
        {
            "title": "orange juice",
            "price": 3.99,
            "quantity": 1
        },
        {
            "title": "rice",
            "price": 1.99,
            "quantity": 3

The result I want to get is 9.96. But I'm struggling to come up with a proper way to do so.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Using a for loop

let totalPrice = 0

shoppingCart.items.forEach(item => {
  totalPrice += item.price * item.quantity
})

But in JS it's also useful to get used to different iterators such as reduce. It may appear a bit confusing at first but basically it just loops through the array, applies some logic (that we can add) and returns a single value which is perfect for computing sum.

const totalPrice = shoppingCart.items.reduce((total, item) => total + (item.price * item.quantity), 0)

Hope it helps!

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!