I have an array that contains some of these 3 values: ['daily', 'monthly', 'yearly']
but sometimes the array only contains: ['monthly', 'daily']
and what I want is to get the minimum between these 3 values which is daily and if not there I want monthly and also if it's not there I want the yearly. How can I achieve that ?
You can approach this issue by comparing the values daily, monthly, and yearly alphabetically/by ASCII order
Simplest approach that doesn't rely on alphabetic ordering (and would therefore still work when adding new intervals like hourly):
function getLowest (arr) {
const order = ['daily', 'monthly', 'yearly']
return order.find(val => arr.includes(val))
}
This works because find will return the first matching result.
Here is the function you just need to pass your array it will return a minimum value.
function getMinimum(arr)
{
return arr.includes('daily') ? 'daily' : arr.includes('monthly') ? 'monthly' : arr.includes('yearly') ? 'yearly' : "";
}
var arr1 = ['daily', 'monthly', 'yearly'];
console.log(getMinimum(arr1));