I have a complex set of nested conditional checks that I am working on refining to avoid getting stuck in a nested if{}else{}
loop. To provide context, I am dealing with two objects, CACHE_FILE_AGE_LIMIT
and CACHE_FILE_CURRENT_AGE
, and I am trying to navigate through a nested age comparison structure:
first check the days,
then check hours,
then check minutes,
then check seconds
The objective is to progressively move from one time unit to the next, such as days to hours, and stop once a greater value is encountered. For instance, if the day values are equal, we move on to comparing hours, and if an hour value surpasses the limit, we terminate the evaluation.
For a clearer understanding, please refer to the provided example.
const CACHE_FILE_AGE_LIMIT = {
days: 3,
hours: 6,
minutes: 15,
seconds: 57
}
const CACHE_FILE_CURRENT_AGE = {
days: 3,
hours: 5,
minutes: 14,
seconds: 57
}
// Function to calculate time difference
function timeDiff(ageObj1, ageObj2) {
// Inserted logic for comparison...
}
// Additional code for populating and evaluating limits
// Please refer to the actual example for more details
As illustrated, the evaluation process ceases once it is determined that the age can be discerned based purely on the hours
.
Feel free to modify the values in CACHE_FILE_CURRENT_AGE
to experiment with different scenarios.
--- UPDATE ---
Appreciation to all those who offered solutions. I will need to select one answer to mark the question as resolved.
Check out the demo of the solution here: