Number Comparison javascript

1

Starting a draft for parking collection needs to know how best to calculate the values based on the number of minutes. That way it is not working, I believe I have errors in the logical operators (I'm not familiar with Javascript)

var valor_total = 0.00;

if(tempo_considerado <= 0){
valor_total = 0.00;}
if(tempo_considerado > 0 <= 10){
valor_total = 0.00;}
if(tempo_considerado > 10 <= 15){
valor_total = 2.50;}
if(tempo_considerado > 15 <= 30){
valor_total = 4.00;}
if(tempo_considerado > 30 <= 45){
valor_total = 5.00;}
if(tempo_considerado > 45 <= 60){
valor_total = 6.00;}
if(tempo_considerado > 60){
valor_total = 10.00;}

valor_total = Math.floor(valor_total).toFixed(2);
    
asked by anonymous 16.09.2017 / 23:59

3 answers

2

You do not need to do the AND comparison. In this case you can use else. would be

if(tempo_considerado <= 0){
    valor_total = 0.00;}
else if(tempo_considerado<= 10){
   valor_total = 0.00;}
else if(tempo_considerado<= 15){
   valor_total = 2.50;}
else if(tempo_considerado<= 30){
  valor_total = 4.00;} 
else if(tempo_considerado<= 45){
  valor_total = 5.00;} 
else if(tempo_considerado<= 60){
  valor_total = 6.00;}
else if(tempo_considerado > 60){
  valor_total = 10.00;}
    
17.09.2017 / 01:37
4

Well, the logical operator AND (& &) and OR (||) were missing, and the code is not declaring tempo_considerado

function myfuction(){
 var valor_total = 0.00;
 var tempo_considerado = document.getElementById("tempo").value;
 if(tempo_considerado <= 0){
    valor_total = 0.00;}
 if(tempo_considerado > 0 && tempo_considerado<= 10){
   valor_total = 0.00;}
 if(tempo_considerado > 10 && tempo_considerado<= 15){
   valor_total = 2.50;}
 if(tempo_considerado > 15 && tempo_considerado<= 30){
  valor_total = 4.00;}
 if(tempo_considerado > 30 && tempo_considerado<= 45){
  valor_total = 5.00;}
 if(tempo_considerado > 45 && tempo_considerado<= 60){
  valor_total = 6.00;}
 if(tempo_considerado > 60){
  valor_total = 10.00;}

 valor_total = Math.floor(valor_total).toFixed(2);
 alert(valor_total);
}
<input type="text"  id="tempo"/>
<input type="button" onclick="myfuction();" value="ok"/>
    
17.09.2017 / 00:17
2

Forgot by the logical operator in ifs with two clauses! It would:

if(tempo_considerado > 0 && tempo_considerado <= 10) //caso queira que o tempo seja maior que zero E menor ou igual a 10

or

if(tempo_considerado > 0 || tempo_considerado <= 10) //caso queira que o tempo seja maior que zero OU menor ou igual a 10.
    
17.09.2017 / 00:15