Skip to content
Permalink
cae34b63aa
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
35 lines (28 sloc) 603 Bytes
'use strict'
let data = []
module.exports.clear = () => {
data = []
}
module.exports.add = (item, qty) => {
qty = Number(qty)
if(isNaN(qty)) throw new Error('the quantity must be a number')
let flag = false
for(let index in data) {
if (data[index].item === item) {
data[index].qty+= qty
flag = true
}
}
if(flag === false) {
data.push({item: item, qty: qty})
}
}
module.exports.getAll = () => {
for(const key in data) data[key].key = key
return data
}
module.exports.delete = key => {
console.log(`delete key ${key}`)
return
}
module.exports.countItems = () => data.length