Why does NaN return? and how to return Number?

1

Why does the following code return NaN (Not a Number)? - and how to return the result of (balance) * (index) in number?

function ContaPoupanca(){
            this.saldo = 0;
            this.deposita = function(valor){
                this.saldo += valor;
            };
            this.atualiza = function(indice){
                this.saldo += this.saldo * this.indice;
            };
        };
var contaPoupanca = new ContaPoupanca();
contaPoupanca.deposita(1000);
console.log(contaPoupanca.saldo);
contaPoupanca.atualiza(0.05);
console.log(contaPoupanca.saldo);
    
asked by anonymous 23.05.2018 / 19:34

1 answer

5

The error is in this.indice . You are trying to access a property of your ContaPoupanca object that does not exist, instead of accessing the function parameter. The correct in this case is only indice . See:

function ContaPoupanca(){
            this.saldo = 0;
            this.deposita = function(valor){
                this.saldo += valor;
            };
            this.atualiza = function(indice){
                this.saldo += this.saldo * indice;
            };
        };
var contaPoupanca = new ContaPoupanca();
contaPoupanca.deposita(1000);
console.log(contaPoupanca.saldo);
contaPoupanca.atualiza(0.05);
console.log(contaPoupanca.saldo);
    
23.05.2018 / 19:36