Anthony N.
Anthony N.

Reputation: 395

Better way of writing the switch

Any shorter (or more efficient) way to write this . just to be precise, daily, weekly... are being fetched from the interface (input[select])

switch(repeatFrequency) {
                case "daily":
                    diffDays = 1;
                    break;
                case "weekly":
                    diffDays = 7;
                    break;
                case "weekly_2":
                    diffDays = 14;
                    break;
                case "monthly":
                    diffDays=31;//we assume the worst case
                    break;
                case "monthly_3":
                    diffDays = 92;//we assume the worst case
                    break;
}

Upvotes: 1

Views: 57

Answers (1)

hindmost
hindmost

Reputation: 7195

The better way is use hash (map) object. Something like this:

var hash = {
    'daily': 1,
    'weekly': 7,
    'weekly_2': 14,
    'monthly': 31,
    'monthly_3': 93,
};
var diffDays = repeatFrequency in hash? hash[repeatFrequency] : default_value;

Upvotes: 1

Related Questions