Numeros javascript comparison

Asked

Viewed 246 times

1

Starting a draft for parking charge I need to know the best way to calculate the values based on the amount of minutes. So it’s not working, I believe I have errors in 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);
  • 1

    Nãu would be so if(time_considered > 0 && time_considered <= 10)

  • 1

    the tempo considerado was not considered, ie was not declared :)

  • Actually the time_considered variable comes from a previous code block

  • there are who think that Stackoverflow users have telepathic magical powers and are able to guess exactly what is happening in the environment of those who ask. They believe that these superpowered beings can guess what the code is and all the other information they need without having to see anything about it and are still able to figure out what’s wrong with it. Thus, these users believe that it is not necessary to place this information in the text of the question because it can be obtained by those who answer through supernatural and paranormal means.

3 answers

4

Well lacked the logic operator AND(&&) or OR(||), and in the code does not declare the 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"/>

  • In fact the variable time_considered comes from another code block earlier. But I got your explanation cool

  • @Ezequieltavares excuse this information had not so I could not guess, but it worked ?

  • I came in the market when I get home testo. But I think I should put breaks in the if’s

  • Well there I do not know informs you I would use 'E-if', but say it is the best way I am not the right person

2

Forgot to put the logical operator in ifs with two clauses! Seria:

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.

2


You don’t need to compare AND. In this case you can use Echo. would stay

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;}

Browser other questions tagged

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