Looking for advice on refactoring this code to reduce the number of if statements. The challenge is handling 4 different inputs to generate a single answer.
const alert= markers.some((marker) => marker['hasAlerts'] > 0);
const warning= markers.some((marker) => marker['hasWarnings'] > 0);
const isCurrentHourAlert = markers.some((marker) => marker['isCurrentHourAlert'] > 0);
const isCurrentHourWarnings = markers.some((marker) => marker['isCurrentHourWarnings'] > 0);
let style = determineStyle(isCurrentHourAlert, isCurrentHourWarnings, alert, warning);
function determineStyle(hasHourAlert, hasHourWarning, hasAlert, hasWarning) {
if (hasHourAlert && hasHourWarning) {
return 4;
} else if (hasHourAlert) {
return 4;
} else if (hasHourWarning) {
return 5;
} else if (hasAlert && hasWarning) {
return 2;
} else if (hasAlert) {
return 2;
} else if (hasWarning) {
return 3;
} else {
return 1;
}
}
Attempted to use switch case statement but still ended up with complex validation in the code.
Interested in exploring patterns or structures that could simplify readability and refactoring of the code.