Ansari iftekhar
Ansari iftekhar

Reputation: 11

Double in reduce method

Here the output should be 63000 but the output is only 18000 ,it means it calculates only last element of the array...please helpenter code here I know it may be a simple question but i am beginnings so please help me

const cart =[
  {proId:1,proName:"mobile",proQuan: 1,proPrice:10000},
    {proId:2,proName:"laptop",proQuan: 2,proPrice:11000},
    {proId:3,proName:"watch",proQuan: 1,proPrice:13000},
    {proId:4,proName:"wrist band",proQuan: 3,proPrice:6000}
];
const totalprice= cart.reduce((acc,curr)=>{
  return acc =  curr.proPrice * curr.proQuan;
},0);
console.log(totalprice);

Upvotes: 0

Views: 177

Answers (1)

7-zete-7
7-zete-7

Reputation: 745

You miss adding to acc:

const cart =[
  {proId:1,proName:"mobile",proQuan: 1,proPrice:10000},
    {proId:2,proName:"laptop",proQuan: 2,proPrice:11000},
    {proId:3,proName:"watch",proQuan: 1,proPrice:13000},
    {proId:4,proName:"wrist band",proQuan: 3,proPrice:6000}
];
const totalprice= cart.reduce((acc,curr)=>{
  return acc + curr.proPrice * curr.proQuan;
},0);
console.log(totalprice);

Upvotes: 3

Related Questions