-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
40 lines (36 loc) · 1.06 KB
/
calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const fs = require('fs')
const basePriceObj = require('./basePriceObj.js')
module.exports = {
calculator,
findMatch,
totalCart,
}
function calculator(base, cart) {
let basePrice = basePriceObj.priceObject(base)
findMatch(basePrice, cart)
return basePrice
}
function findMatch(base, cart) {
let itemToFind = []
cart.forEach(items => {
let itemName = items['product-type']
let itemSize = items.options.size
let itemColour = items.options.colour
let itemFind = ((itemSize ? itemSize : '') + (itemColour ? itemColour : ''))
itemToFind.push(base[itemName][itemFind])
cartPrice = base[itemName][itemFind]
})
totalCart(itemToFind, cart)
return itemToFind
}
function totalCart(items, cart) {
var add = (a, b) => a + b;
var individualPrice = []
items.forEach((productCost, i) => {
let artistMarkup = (cart[i]['artist-markup'] / 100) * productCost
individualPrice.push((productCost + Math.round(artistMarkup)) * cart[i].quantity)
})
let totalArray = individualPrice.reduce(add, 0)
console.log(totalArray)
return totalArray
}