houcin olmostaf
houcin olmostaf

Reputation: 201

reduce() method in JavaScript does not work

I having to issues with my code. the first one is that I am trying to format The Total as currency but it does not work. my second issues is I am using the Reduce() method to track and calculate the price when ever the user selects the item till here it works find, but when the user unselects an item it goes back to zero . I do not that I just want to mince it and keep the selected once .

const items = useSelector((state) => state.cartReducers.selectedItems.items);
 
const Total = items
  .map((item) => Number(item.price.replace("$", "")))
  .reduce((prev, curr) => prev + curr, 0);
    
const USD = Total.toLocaleString("en", {
  style: "currency",
  currency: "USD",
});

Upvotes: 0

Views: 758

Answers (1)

Charfeddine Mohamed Ali
Charfeddine Mohamed Ali

Reputation: 1106

Try to change en to en-US

 const USD = Total.toLocaleString('en-US',
  {style: 'currency', currency: 'USD'}
);

Upvotes: 2

Related Questions