Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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];
about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

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)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda