I am working in this project where I do an offer of a job
The class Offer has 4 parameters (Name, State, Detail and Price). However, in my table I have 5 columns (Name, State, Detail, Price and Type). Where I have to show '$$$$' for an offer's price to be bigger than 75% of the min and max values of array (see later), I also have to add '$$$', '$$' and '$' (not a problem).
When I add the first offer it shows a '-' because min and max can't be equal. So far so good. The problem is when I have to add a second, third, etc. offers since it uses the last price to calculate the percentage and this causes old offer types to miscalculate.
Code:
tipodeprecio(){
let num = 0;
let Tipo = [];
for(let i=0; i< this.listaOferta.length; i++){
num = this.listaOferta[i].Precio;
Tipo.push(num);
}
let min = Math.min(...Tipo);
let max = Math.max(...Tipo);
let dif = max - min;
let porcentaje = (num*100)/dif;
if(min === max){
return "-";
} else{
if (porcentaje > 75){
return "$$$$";
}
else if (porcentaje <= 75 && porcentaje > 50){
return "$$$";
}
else if (porcentaje <= 50 && porcentaje > 25){
return "$$";
}
else {
return "$";
}
}
}