How to implement the function?

I do not understand where is the error in the function(
var items = ['0','1','2','3','4'];
items[0] = {name:'Screwdriver',price:'150'};
items[1] = {name:'Key',price:'100'};
items[2] = {name:'Chisel',price:'180'};
items[3] = {name:'File',price:'200'};
items[4] = {name:'Hammer',price:'500'};
console.log('Buy',items[4].name,'price',items[4].price,'RUB');
var x;
var discount;
showDiscount function(items, x) {
if (x < 10) {
 discount = 5;
}
else if (10 < x < 50) {
 discount = 7;
}
else if (50 < x < 100) {
 discount = 10;
}
else if (x > 100) {
 discount = 15;
}
var result = x*(prices-0.01*prices*discount);
var benefit = x*0.01*prices*discount;
console.log(items.name'- the cost of the party of',x,'items',items.price,'RUB (discount',discount,'%), your benefit',benefit,'RUB');
}
showDiscount([0],150);
June 27th 19 at 14:56
2 answers
June 27th 19 at 14:58
I would do a little differently. The idea is that you have to specify info about a quantity remaining of the product for each item(quantity). Assuming that you do, it can be rewritten as follows

const items = [
 {name:'Screwdriver',price:'150', quantity: 100},
 {name:'Key',price:'100', quantity: 200},
 {name:'Chisel',price:'180', quantity: 500},
 {name:'File',price:'200', quantity: 1000},
 {name:'Hammer',price:'500', quantity: 250}
];

function getDiscount(x) {
 if(x < 10) return 5;
 if(x > 10 && x < 50) return 7;
 if(x > 50 && x < 100) return 10;
 if(x > 100) return 15;
}

getCustomerBenefit function(item) {
 const benefit = item.quantity * 0.01 * item.price * getDiscount(item.quantity);

 console.log('money you can save due to discount', benefit);

 return benefit;
}

calculateTotalPayment function(item) {
 const beforeDiscount = item.quantity * item.price;
 const totalPayment = beforeDiscount - getCustomerBenefit(item);

 console.log('beforeDiscount', beforeDiscount);
 console.log('total payment(discount included)', totalPayment);

 return totalPayment;
}

calculateTotalPayment(items[4]);


p.s. https://repl.it/HCNy
Thank you! - loren.Daughert commented on June 27th 19 at 15:01
I would also rewrite the function to discount that in the future it was easier to maintain:
discount function(x) {
 var quantity=[10,50,100];
 var discount=[5,7,10,15];
 return discount[quantity.reduce(function(prev, cur) {
 return x >= cur ? prev + 1 : prev
 }, 0)];
}
- Edgar.Lesch commented on June 27th 19 at 15:04
: Yes, there is actually much improvement, I just wanted to show a more familiar option

p.s. looks too difficult, to be honest, I don't think is worth it - Dianna.Harris commented on June 27th 19 at 15:07
June 27th 19 at 15:00
showDiscount([0],150);
[0] later in the variable items . as a result, the information on the product
How, then, to call the object at index 0? - loren.Daughert commented on June 27th 19 at 15:03
: showDiscount(items[0],150); - Edgar.Lesch commented on June 27th 19 at 15:06
Happened, but the discount shows 7% and should be 15%, and the benefit of NaN - Dianna.Harris commented on June 27th 19 at 15:09

Find more questions by tags JavaScript