TheCodedMaster Cubing
TheCodedMaster Cubing

Reputation: 129

Is there a way to shorten a mostly repetitive if statement?

I'm trying to make a number to month conversion. I'm trying to do it with an if statement, e.g. if(num===0) {month = "January";} else if(num===1) {month = "February";} etc. Is there a way to shorten this? I imagine it'd be something like if(num===[0,1,2,3,4,5]) {month = ["January", "February", "March", "April", "May", "June"}

In other words, is there some kind of way to use an array with an if function, while the if function goes through each one respectively? I am using the p5 library, by the way, if that makes any difference.

Upvotes: 0

Views: 204

Answers (5)

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

There are better ways than a hardcoded switch.

let locale = 'en-us'

let month = 3 // Apr

let months = [...Array(12).keys()].map(v => new Date(2000, v).toLocaleString(locale, {
  month: 'long'
}))

if (months[month]) {
  console.log('Do something...')
}

// some other examples

let result;

//
result = months[month] || 'Invalid month index'
console.log('by index', result)

//
result = months[month - 1] || 'Invalid month number'
console.log('by month number', result)

//
result = months.findIndex(v => v === new Date().toLocaleString(locale, { month: 'long' }))
console.log('find index by name', result)
console.log('use index to get name', months[result])

Upvotes: 0

cj-2307
cj-2307

Reputation: 311

I think the best solution would be to use a array of months like this:

const month = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
];

const answer = month[num];

Upvotes: 1

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2885

if ur ranges are mostly contiguous in nature, one way is to pre-make a lookup string that's also simultaneously the keys and the values, like

1:JAN:2:FEB:3:MAR: etc

with a constant-width nature. even 1:J:2:F:3:M:4:A….(i mean it's feasible to type in all the names there but then you'll need some padding).

and when then function is called, use a combination of match or substring functions to "cut out" the ones u need first, say "4:APR:5:MAY:6:JUN:7:JUL" (you'll have to be creative with ur own notations when u're using single letters each and when 3-5, 4-8 and 1-6-7 duplicate… or just use base-12)

if a 3-letter prefix already satisfy ur needs, then do a high-speed gsub to get rid of all the numbers and delimiters, and change them to a comma or something.

and if u opt for the full months, i dunno how much it'll help at all, but perhaps u can regex sub-in the common suffixes of 9-10-11-12 mostly in one shot, and also 1-2. 5 is already its full name.

another approach is that if ur ranges generally span more than half the year, then even a "subtraction" approach might be preferable to an "addition one" - always start with the full string with all months spelt out. then for the months missing from ur input, blank those out, and return what's leftover.

and say if ur ranges tend to cluster within the same half of the year instead of straddling, then u can even split that index key into 2 to make it faster(the savings are tiny though)

Upvotes: 0

Eric Marceau
Eric Marceau

Reputation: 1707

Create an awk definition as follows:

getMonth()
{
    awk 'BEGIN{
        months=[January,February,March,April,May,June,July,August,September,October,November,December] ;
    }{
        print months[$1] ;
    }'
}

Then process your input using a call

month=`echo ${digit} | getMonth `

where digit is assigned a value from 1 to 12.

Upvotes: -1

ErykK
ErykK

Reputation: 28

You can use a switch statement

var num = 0;
  switch (num) {
    case 0:
      month = "January";
      break;
    case 1:
      month = "February";
      break;
    case 2:
      month = "March";
      break;
    case 3:
      month = "April";
      break;
    case 4:
      month = "May";
      break;
    case 5:
      month = "June";
      break;
    default:
      day = "Unknown Month";
  }

Read More: https://www.w3schools.com/jsref/jsref_switch.asp https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

Upvotes: 1

Related Questions