Parking activity in JAVASCRIPT

Asked

Viewed 228 times

-3

I need to do a class exercise from a parking system, follow the HTML format:

<head>
    <meta charset="utf-8"/>
    <title>Estacionamento</title>
    <script type="text/javascript" src="jquery.js"></script>
    <style type="text/css">
        fieldset { width:250px; }
        label { display:block; float:left; } 
        label, input, textarea, select { width:150px; margin: 5px 0; }
        th, td { border:1px solid #ccc; font-size:0.9em; }
    </style>
</head>
<body>

    <form id="formulario">
        <fieldset>
            <legend>Estacionamento</legend>

            <label for="tempoPermanencia" >Informe o tempo de permanencia:</label>
            <input type="text" id="tempoPermanencia" name="tempoPermanencia" /><br /><br />

            <button type="button" id="button" onclick="calcular()">Calcular</button><br /><br />

            <label for="total" >Total a pagar:</label>
            <input type="text" id="total" name="total" readonly="readonly"/><br /><br />    

        </fieldset>
    </form>
<script type="text/javascript" src="estacionamento.js"></script>
</body>

The rules of parking are:

3,00 every hour 30.00 daily

# A partir de 3hs 4,00 a cada 30 min

var array = [];

function calcular(){
    var tempoPermanencia = $("#tempoPermanencia").val();
    var totalAcumulado = 0;


var estacionamento = new Object();
estacionamento['tempoPermanencia'] = tempoPermanencia;
estacionamento['totalAcumulado'] = totalAcumulado;


array.push(estacionamento);

for (i = 0; i < array.length; i++){

    var hora = $("#tempoPermanencia").val().split(":");


    if (array[i].tempoPermanencia >= "01:00"){

        if(hora[1] == "00"){
            totalAcumulado += 5 * hora[0];
        }
        else if(hora[1] != "00"){
            totalAcumulado += (5 * hora[0]) + 2.5;

            console.log(hora);

        }

    }else if (array[i].tempoPermanencia == "00:30"){

        totalAcumulado += 2.5;
    }
    else if (array[i].tempoPermanencia == "24:00"){

        totalAcumulado += 50;

    }
    else if (array[i].tempoPermanencia >= "03:00"){         

        if(hora[1] == "00"){ 
            totalAcumulado += (hora[0] * 2) * 2;

            console.log(hora);

        }else if(hora[1] != "00"){
            totalAcumulado += (hora[0] * 2 + 1) * 2;

            console.log(hora);

        }

    }
//$("#total").val(totalAcumulado);
//$("#tempoPermanencia").val("");
//$("#tempoPermanencia").focus();

}
$("#total").val(totalAcumulado);
$("#tempoPermanencia").val("");
$("#tempoPermanencia").focus(); 
}

I did it this way but I would like to check if there is a more efficient way, I believe mine is making mistakes too... Thank you all

  • Do you "believe it contains errors"? The teacher did not teach you to check for errors on the console?

  • Errors in not being 100% meeting what the exercise asks for... Not showing syntax error

1 answer

1

I found your logic confusing, my suggestion would be to add the hours to the minutes, and work only with the minutes. Then you could calculate the total that way:

var aPagar = 0;

// Se ficou mais que 5 horas e meia, paga o teto (diária = 1140 minutos)
while (minutos > 330) {
  aPagar += 30
  minutos -= 1140
}

// Se ficou mais que 3 horas, paga R$4 por 30 minutos 
while (minutos > 180) {
  aPagar += 4
  minutos -= 30
}

// Finalmente, cobra $3 por hora
while (minutos > 0) {
  aPagar += 3
  minutos -= 60
}

Browser other questions tagged

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