@Maksspb

Как реализовать функцию?

Не пойму где ошибки в функции(
var items = ['0','1','2','3','4'];
items[0] = {name:'Отвертка',price:'150'};
items[1] = {name:'Ключ',price:'100'};
items[2] = {name:'Зубило',price:'180'};
items[3] = {name:'Напильник',price:'200'};
items[4] = {name:'Молоток',price:'500'};
console.log('Купите',items[4].name,'по цене',items[4].price,'руб.');
var x;
var discount;
function showDiscount(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,'- стоимость партии из',x,'штук',items.price,'руб. (скидка',discount,'%), ваша выгода',benefit,'руб.');
}
showDiscount([0],150);
  • Вопрос задан
  • 177 просмотров
Пригласить эксперта
Ответы на вопрос 2
devellopah
@devellopah
я бы сделал немного по-другому. По идее, ты должен указать инфу о кол-ве, оставшегося товара для каждой позиции(quantity). Если предположить, что ты так и сделаешь, то можно переписать так

const items = [
	{name:'Отвертка',price:'150', quantity: 100},
	{name:'Ключ',price:'100', quantity: 200},
	{name:'Зубило',price:'180', quantity: 500},
	{name:'Напильник',price:'200', quantity: 1000},
	{name:'Молоток',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;
}

function getCustomerBenefit(item) {
	const benefit = item.quantity * 0.01 * item.price * getDiscount(item.quantity);
	
	console.log('money you can save due to discount', benefit);
	
	return benefit;
}

function calculateTotalPayment(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
Ответ написан
Arekus
@Arekus
showDiscount([0],150);
[0] в дальнейшем в переменной items . в результате не получаешь информации по товару
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы