«От зарплаты до зарплаты»

let calculateSalary = function (salary) {
if (salary < 100000) {
salary *=0.65;
} else if (salary >= 100000){
salary *=0.55;
}
return salary;
};

Во второй проверке выдаёт 55000.00000000001. В чем ошибка?

salary = Math.round(salary * 0.55); и с 0,65, соответственно так же, не будет ошибок

1 лайк

спасибо, попробую

let calculateSalary = function (i) {
if (i < 100000) {
return i / 100 * 65
} else {
return i / 100 * 55

  }

};

let calculateSalary = function (i) {
let res = 0
if (i < 100000) {
res = i / 100 * 65
} else {
res = i / 100 * 55

  }
  return res

};

let calculateSalary = function (dirtySalary) {
let cleanSalary;
if(dirtySalary>=100000){
cleanSalary = Math.round(dirtySalary-(dirtySalary*0.45));
}else{
cleanSalary = Math.round(dirtySalary-(dirtySalary*0.35));
}
return cleanSalary;
};
Получилось. Так же получилось и без Math.round()

1 лайк
let dirtySalary

let calculateSalary = function (dirtySalary) {
  if (dirtySalary < 100000) {
  return Math.round(dirtySalary * 0.65)
} else { return Math.round(dirtySalary * 0.55) }
};

Дополню свои вариантом:

let calculateSalary = function (salary) {
  
  let percent;
  
  if (salary < 100000) {
      percent = 0.35;
    } else {
      percent = 0.45;  
    }
    
    let clearSalary = Math.round(salary - salary * percent);
    return clearSalary;
};

Мой пример:

let calculateSalary = function (dirtyCash) {
let percent = 0.45;
if (dirtyCash < 100000) {
percent = 0.35;
}
let clearCash = Math.round(dirtyCash * percent);
return dirtyCash - clearCash;
};

let calculateSalary = function (blackSalary) {
let percentNalog;
if (blackSalary < 100000) {
percentNalog = 0.35;
}else {
percentNalog = 0.45;
}
let nalog = blackSalary * percentNalog;
let whiteSalary = blackSalary - nalog;
return Math.round(whiteSalary);
};
calculateSalary();

Получилось решить эту задачу без добавления else в условии сравнения зарплаты со 100 000

let calculateSalary = function (dirtySalary) {
  let percent = 0.35;
  if (dirtySalary >= 100000) {
    percent = 0.45;
  }
  let clearSalary = dirtySalary - (dirtySalary * percent);
  return clearSalary;
};