Untitled
unknown
plain_text
a month ago
1.7 kB
3
Indexable
Never
/* Design a basic eccomerce app with following methods * CreateProduct * CreateOrder * getOrder * CancelOrder * deleteProduct */ let products = [] let orders=[]; let counter = 1; let orderCounter = 0; function createProduct(brand, name, desc, price, quantity){ let pExists = products.find(p => p.name == name && brand == brand ) let product = { "id": counter, "name": name, "desc": desc, "price" : price, "qty": quantity } if(!pExists){ counter = counter + 1 products.push(product) } else{ throw Error("product already exists") } return product; } console.log(createProduct("nokia", "n90", " mobile phone", 12000, 10)) console.log(createProduct("nokia", "n80", " mobile phone", 12000, 10)) console.log(createOrder([{"id": 1, "qty": 5}, {"id": 2, "qty": 1}])) console.log(createOrder([{"id": 1, "qty": 4}, {"id": 2, "qty": 11}])) console.log(createOrder([{"id": 1, "qty": 4}, {"id": 2, "qty": 1}])) function createOrder(orderItems){ let orderObj = []; for (const o of orderItems){ let product = products.find(p => p.id == o.id); if(product.qty >= o.qty){ let tmpProduct = { "name" : product.name, "desc" : product.desc, "price" : product.price, "totalPrice": o.qty * product.price, "qty": o.qty } orderObj.push(tmpProduct) product.qty = product.qty - o.qty } else{ throw Error("product qty exceeds inventory qty") } } return orderObj; }
Leave a Comment