Творческий доход


#1
if (buy >= 1000 && buy < 3000) {
    discountedBuy = buy * (5 / 100);
    discountedBuy = buy - discountedBuy;
  } else if (buy >= 3000 && buy < 5000) {
      discountedBuy = buy * (10 / 100);
      discountedBuy = buy - discountedBuy;
    } else if (buy >= 5000) {
        discountedBuy = buy * (15 / 100);
        discountedBuy = buy - discountedBuy;
      } else {
          discountedBuy = buy;
        }

#2

Можно проще:

var buy = 4000;
var discountedBuy;

if(buy >= 1000 && buy < 3000){
    discountedBuy = buy * 0.95
  }else if(buy >= 3000 && buy < 5000){
      discountedBuy = buy * 0.9
    }else if(buy >= 5000){
        discountedBuy = buy * 0.85
      }else discountedBuy = buy

#3
switch (true) {
  case buy >= 1000 && buy < 3000: discountedBuy = buy * 0.95; break;
  case buy >= 3000 && buy < 5000: discountedBuy = buy * 0.9; break;                 
  case buy >= 5000: discountedBuy = buy * 0.85; break;
  default: discountedBuy = buy;
}

#4

Мой вариант

var buy = 4000;
var discountedBuy;

var discount = 0;

if (buy >= 1000 && buy < 3000) {
discount = 5;
}
else if (buy >= 3000 && buy < 5000) {
discount = 10;
}
else if (buy >= 5000) {
discount = 15;
}

discountedBuy = buy * (100 - discount)/100;


#5

по сути одно и тоже, только у Kyver’a там ошибка, у него всегда по default’у будут результаты)


#6

вот мое решение /*
var buy = 4000;
var discountedBuy;
if (buy>=1000 && buy<3000){
discountedBuy = buy - (buy / 100 * 5);
} else if (buy>=3000 && buy<5000){
discountedBuy = buy - (buy / 100 * 10);
} else if (buy>=5000){
discountedBuy = buy - (buy / 100 * 15);
} else {
console.log(discountedBuy=buy);
}
*/


#7

твой вариант более симпатичен, нежели мой:+1:


#8

у меня так получилось:

var buy = 4000;
var discountedBuy = buy;

if (buy >= 1000 && buy < 3000) {
  discountedBuy = buy * 0.95;
}

if (buy >= 3000 && buy < 5000) {
  discountedBuy = buy * 0.90;
}

if (buy >= 5000) {
  discountedBuy = buy * 0.85;
}

#9
var buy = 999;
var discountedBuy;

if (buy < 1000) {
  console.log('Итого, стоимость ' + (discountedBuy = buy) + 'р.');
}
if (buy >= 1000 && buy < 3000) {
  discountedBuy = buy * 0.05
  if (discountedBuy = buy - discountedBuy) {
  console.log('Итого, стоимость ' + (discountedBuy) + 'р.');
  }
}
if (buy >= 3000 && buy < 5000) {
  discountedBuy = buy * 0.1
  if (discountedBuy = buy - discountedBuy) {
    console.log('Итого, стоимость ' + (discountedBuy) + 'р.');
  }
}
if (buy >= 5000) {
  discountedBuy = buy * 0.15
  if (discountedBuy = buy - discountedBuy) {
    console.log('Итого, стоимость ' + (discountedBuy) + 'р.');
  }
}