Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // If you're using an array of objects you should use this one.
- let arrayOfObjects = [
- { name: "paper", quantity: 100, unitPrice: 1.96 },
- { name: "pen", quantity: 57, unitPrice: 1.22 },
- { name: "eraser", quantity: 6, unitPrice: .35 }
- ]
- let totalObjectsPrice = arrayOfObjects.reduce((acc, { quantity, unitPrice }) => {
- return acc + Math.round(quantity * unitPrice);
- }, 0);
- // If you're using an array of integers you should use this one.
- let arrayOfNumbers = [196, 69.53, 2.09];
- let totalNumbersPrice = arrayOfNumbers.reduce((prev, next) => Math.round(prev + next));
- console.log(["array of objects price: ", totalObjectsPrice, " array of numbers price: ", totalNumbersPrice])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement