Solución
solution.tsTypeScript
const RESP_INVALID = -1
const ZERO = 0
const ONE_HUNDRED = 100
const SENIOR = "senior"
const BONUS_SENIOR = 0.20
const MID = "mid"
const BONUS_MID = 0.10
const UPPER_SALARY = 5000
const UPPER_SALARY_TAX = 0.25
const MID_SALARY = 2000
const MID_SALARY_TAX = 0.15
const BASE_SALARY_TAX = 0.08
function calcBonus(category: string, salary: number) {
const mapBonus = {
[SENIOR]: salary * BONUS_SENIOR,
[MID]: salary * BONUS_MID
}
return mapBonus[category] ?? ZERO
}
function calcTax(salary: number) {
if (salary > UPPER_SALARY) {
return salary * UPPER_SALARY_TAX;
}
if (salary > MID_SALARY) {
return salary * MID_SALARY_TAX;
}
return salary * BASE_SALARY_TAX;
}
export function calculateMonthlySalary(hours: number, rate: number, category: string): number {
// Verificar si las horas trabajadas son válidas
// Verificar si la tarifa por hora es válida
if (hours <= ZERO || rate <= ZERO) return RESP_INVALID;
// Calcular salario bruto multiplicando horas por tarifa
const gross_salary = hours * rate;
const bonus = calcBonus(category, gross_salary)
// Sumar salario bruto más bonificación
const total_salary = gross_salary + bonus;
const tax = calcTax(total_salary)
// Restar impuestos y redondear a dos decimales
return Math.round((total_salary - tax) * ONE_HUNDRED) / ONE_HUNDRED;
}0respuestas