Is there any way to simplify my structure? if yes how

Asked

Viewed 177 times

-4

I was able to solve this question more I wanted to know if I can simplify it because I am in the middle of learning and I look for different ways to do the same thing to be able to fix it well in the mind. the question is as follows.

A parking lot wants to automate the collection of monthly messages.

If the driver held up to 20 entries, he must pay $ 10,00 per admission held.

From the twenty-first entry onwards, each entry costs R $ 5,00 to the customer.

//array que recebe as placas dos veiculos

var placas = [
    'RXB-2525', 'AKX-3333', 'ORO-7142','RXB-2525', 'AKX-3333', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333', 
    'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'ORO-7142', 'ORO-7142',
    'ORO-7142', 'RXB-2525', 'AKX-3333','AKX-3333', 'ORO-7142', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333',
    'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525'
 ]
 
//aqui calcula quantas vezes essa mesma placa entrou no estacionamento

 function calcularNumeroDeEntradas(placa){

    var entradas = 0

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

       if (placas[i] == placa){

           entradas++

       }

    }

    return entradas

 }

//aqui calcula o valor que o dono do carro deve pagar, E aqui que queria simplificar o código

 function calcularValorDevido(placa){

   var valorDevido = calcularNumeroDeEntradas(placa)

    if (valorDevido < 21){

       return valorDevido = valorDevido*10

    } else{

       return valorDevido = ((valorDevido-20)*5) + (20*10)

    }

}

 console.log(calcularNumeroDeEntradas("ORO-7142"))

 console.log(calcularValorDevido("ORO-7142"))

1 answer

2


It depends on your definition of "simplify", which is simple for me may not be simple for you, here is my solution to your problem which I believe is "simple".

You will need methods like Math.min which returns at least between 2 or more elements passed as parameter, which in your case I used with (20, entrada), i.e., if the number of entries is greater than 20, it will return 20, if it does not return the number of entries.

I also used the method filter that filters an array based on the function passed as parameter and returns a new array with all the elements that passed the test, which in this case I used to create a new array with all the elements that have the same board and then I just take the length of this new array that corresponds to the number of entries.

Remarks:

The array placas may not exist, so in this case you can add a ternary operator to avoid errors, for example placas.filter you’ll have to make a mistake if placas does not exist as it will try to apply the method filter at a value that does not exist, so would be like this with the operator placas?.filter in this case if the array does not exist it will return null and so we add a control to the end of the expression ?? 0 that is if the value on the left of the filter for some reason returns null, then it uses 0 as default value.

var placas = [
    'RXB-2525', 'AKX-3333', 'ORO-7142','RXB-2525', 'AKX-3333', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333', 
    'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'ORO-7142', 'ORO-7142',
    'ORO-7142', 'RXB-2525', 'AKX-3333','AKX-3333', 'ORO-7142', 'ORO-7142',
    'AKX-3333', 'RXB-2525', 'AKX-3333','AKX-3333', 'RXB-2525', 'AKX-3333',
    'RXB-2525', 'AKX-3333', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'ORO-7142', 'ORO-7142','AKX-3333', 'AKX-3333', 'RXB-2525',
    'AKX-3333', 'AKX-3333', 'RXB-2525','AKX-3333', 'AKX-3333', 'RXB-2525'
 ];

 function calcDivida(placa){
    // Procura o número de ocorrencias desta mesma placa dentro ao array
    const entrada =  placas?.filter(p => p === placa).length ?? 0;

    // Controlamos se houveram entradas adicionais usando o operador ternario, basicamente um if/else em uma linha como podes ver
    const entradasAdicionais = entrada < 21 ? 0 : entrada - 20;
    
    // O valor inicial é dado do minimo entre 20 e o número de entradas multiplicado por 10
    const valorInicial = Math.min(20, entrada) * 10;
    
    // O valor adicional é o valor salvado antes multiplicado por 5
    const valorAdicional = entradasAdicionais * 5;
    
    // retornamos a soma de ambos
    return valorInicial + valorAdicional;
 }
 
 console.log(calcDivida('RXB-2525')); // 170 
 console.log(calcDivida('AKX-3333')); // 270

  • I think I put myself wrong on the simple question, but it was that same other ways to do the same thing, helped me a lot has things I need to go deeper, and the way you answered showed me another way to solve the problem and even more beautiful to see kkkk XD.

Browser other questions tagged

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