Is there any way to replace these ifs?

Asked

Viewed 279 times

2

  if(inputUsuario >= 16.15 && inputUsuario < 16.20){
    inputUsuario = 16.20;
  }    
  if(inputUsuario >= 16.00 && inputUsuario < 16.15){
    inputUsuario = 16.15;
  } 
  
  if(inputUsuario >= 15.55 && inputUsuario < 16.00){
    inputUsuario= 16.00;
  }     
  if(inputUsuario >= 15.50 && inputUsuario < 15.55){
    inputUsuario = 15.55;
  } 
  if(inputUsuario >= 15.45 && inputUsuario < 15.50){
    inputUsuario = 15.50;
  }  

This code above is an adaptation I made of something I’m developing. In this case, if the user enters a value between 15.45 and 15.50, I have to transform his input into 15.50.

Example: The user typed 16.16, so I have to turn that value into 16.20.

The big problem is that the user can enter a value between 07.00 and 23.30 and for each of these values I need to add an if to make this modification. Is there any way I can replace these ifs?

I use the final value of input to search a spreadsheet, for this reason I need to modify the value this way. The big problem is that there is no pattern.

If there was a pattern, there would be some way to decrease these ifs? For example, if the inputs were 0.30 on 0.30.

  • From what I understand, looks like have a certain pattern: at each interval of 0.05, rounded upwards to 2 decimal places of accuracy, or so (except in the case of 16.00 and 16.15, round to 16.15). Anyway, just with these examples, this is what you can "guess"...

  • @hkotsubo, thanks for the help =D. But it is quite bizarre, sometimes the numbers appear from 0.30 on 0.30, sometimes and 0.05 on 0.05, so there is a very well defined pattern.

  • That would happen to be hours?

  • Guidance for those who want to publish a reply: the result of the operation must be the own input value if this is not contained within the adjustment range.

3 answers

6


I couldn’t see a pattern so you have to handle it individually. If you have a pattern the question is all wrong.

What you can do is create a loop and use a table with the minimum and maximum values, decreases the code, but the treatment will be individual.

As the question does not give more details I assumed some things, but is generating the result waiting. Using the code snippet posted (need to finish the other tracks):

const tabela = [ { min : 15.45, max : 15.5 },
                 { min : 15.5, max : 15.55 },
                 { min : 15.55, max : 16 },
                 { min : 16, max : 16.15 },
                 { min : 16.15, max : 16.2 } ];
var input = 16.16;
var valor = 0;
for (var i = 0; i < tabela.length; i++) {
    if (input >= tabela[i].min && input < tabela[i].max) {
        valor = tabela[i].max;
        break;
    }
}
console.log(valor);

I put in the Github for future reference.

The value 0 that I put would be a default value to adopt if it does not fall into any range. This value needs to be adjusted as needed or even do some checking of what to do when not fall into some range. The question says nothing about it and the excerpt posted on it does not show what to do in this case.

If you had a pattern, a regular interval, was always equal to the maximum of the range (and it looks like it is, but without a pattern in the range) then you could use math and kill all or almost all of the ifs.

  • The conversation went on and on moved to the chat - comments out of context and flagged have been removed. Anyone who wants to proceed or follow, can click on the given link.

1

I noticed a pattern yes.

I’d be more or less like that logic:

function adaptar(value) {
  // tratar exceções
  if (value >= 16 && value < 16.15) {
    return "16.15";
  }

  const inteiro = (value * 100);
  const modulo = inteiro % 5;
  if (modulo == 0) {
    return (inteiro / 100).toFixed(2);
  }
  const soma = 5 - modulo;
  return ((inteiro + soma) / 100).toFixed(2);
}

function test() {
  let input = document.getElementById("input");
  let resp = document.getElementById("resp");
  if (!input.checkValidity()) {
    input.reportValidity();
  } else {
    resp.innerHTML = adaptar(input.value);
  }

}
<input type="number" id="input" oninput="test()" min="7" max="23.3" maxlenght="5" step="0.01">
<span id="resp"></span>

-2

Functional programming is beautiful!
Everything you wanted with none Ifs.

const tabela = [ { min : 15.45, max : 15.5 },
                 { min : 15.5, max : 15.55 },
                 { min : 15.55, max : 16 },
                 { min : 16, max : 16.15 },
                 { min : 16.15, max : 16.2 } ];
    var input = 16.16;
    var valor = tabela.filter((num) => {
        return input >= num.min && input < num.max;
    })[0].max;
    console.log(valor)

  • 2

    could also be without these two hundred rows of the table

  • 2

    Mixing var and const. You know at least the difference between one and the other?

  • 1

    The purpose of this answer was only to demonstrate the functional programming technique using filter in an array and not to discuss types of variables/constants, as for the data object, in a real application it can come in several different ways.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.