Calculate interest rate with Javascript

Asked

Viewed 474 times

0

I’m not able to calculate the interest rate of a variable with Javascript (I started studying today)

Follows the code:

<h1>FORMULÁRIO</h1>
<label for ="id">Id</label>
<input id ="id" name ="id" type="number" ng-model="cadastro.id"/>
<br>
<br>
<label for ="nome">Nome Cliente</label>
<input id ="nome" name ="nome" type="text" ng-model="cadastro.nome"/>
<br>
<br>
<label for ="limite">Limite de Crédito</label>
<input id ="limite" name ="limite" type="number" ng-model="cadastro.limiteCredito"/>
<br>
<br>
<label for ="risco">Risco</label>
<input id ="risco" name ="risco" type="text" ng-model="cadastro.risco"/>
<br>
<br>
<button ng-click="salvar(cadastro)"> SALVAR </button>
<button ng-click="cancelar(cadastro)"> CANCELAR </button>
<br>
<br>
</form>
$scope.salvar = function (cadastro) {

    /*
     *Lógica para podermos utilizar a function Editar.
     */
    var achouCadastro = false;
    for (var i = 0, length = cadastros.length; i < length; i++) {
        if (cadastros[i].id === cadastro.id) {
            cadastros[i].nome = cadastro.nome;
            cadastros[i].limiteCredito = cadastro.limiteCredito;
            cadastros[i].risco = cadastro.risco;
            achouCadastro = true;
            break;
        }
    }

    if (!achouCadastro) {

        /*
         * Lógica para calcular a taxa de juros 
         */

        var valorJuros = cadastro.risco.value;
        var limiteComJuros = number(cadastro.limiteCredito.value);
        if (valorJuros === "A") {
            return limiteComJuros;
        } else if (valorJuros === "B") {
            limiteComJuros = cadastro.limiteCredito + cadastro.limiteCredito * 0, 1;
        } else {
            limiteComJuros = cadastro.limiteCredito + cadastro.limiteCredito * 0, 2;
        } 
        cadastros.push(cadastro);

    }
    $scope.cadastro = {};

};

Man if .. else is not working. Someone could give me a light?

1 answer

1

I saw that you are trying to use the function to store the values in arrays later, however, the calculation of interest based on the data that were entered in the form field, the simple logic would be :

var valorJuros = cadastro.risco.value;
        var limiteComJuros = parseFloat(cadastro.limiteCredito.value);
        if (valorJuros === "A") {
            return limiteComJuros;
        } else if (valorJuros === "B") {
            limiteComJuros = cadastro.limiteCredito + cadastro.limiteCredito * 0.1;
        } else {
            limiteComJuros = cadastro.limiteCredito + cadastro.limiteCredito * 0.2;
        } 
  1. parseFloat() will always return in number, so you won’t run the risk of picking up values that JS won’t be able to calculate
  2. Multiply any value and date by 0 (cadas.limiteCredito * 0, 1), you will have as a result, 0.

If the above logic is not what you are looking for, comment on the logic you are waiting for the results and I will try to redo the code to help you.

Browser other questions tagged

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