Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Uncaught TypeError: this._somatorio is not a function

Bom dia a todos,

Estou implementando o codigo abaixo, mas está dando o seguinte erro:

Aula05.js:16 Uncaught TypeError: this._somatorio is not a function
    at Function.chamaSomatorio (Aula05.js:16)
    at index.html:11

Uncaught TypeError: this._somatorio is not a function. Como corrigir?

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>        
        <script src="Aula05.js"></script>
        <script>
    <h1>Teste Somatório</h1>
            let arrayNumeros = [1,2,3,4,5];
            console.log(Aula05.chamaSomatorio(arrayNumeros));
        </script>
    </body>
</html>

Aula05.js

class Aula05{

    constructor(){
    }

    _somatorio(arrayNumeros) {
        var resultado = 0;
        arrayNumeros.forEach(elemento => {
            resultado += elemento;
        });
        return resultado;
    }

    static chamaSomatorio(arrayNumeros){
        return this._somatorio([1,2,3,4]);
    }
}
2 respostas
solução!

Vinícius,

tira o H1 de dentro da TAG SCRIPT e no JS declara _somatorio static também.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>        
        <script src="Aula05.js"></script>
    <h1>Teste Somatório</h1>
        <script>
            let arrayNumeros = [1,2,3,4,5];
            console.log(Aula05.chamaSomatorio(arrayNumeros));
        </script>
    </body>
</html>
class Aula05 {

    constructor(){
    }

    static _somatorio(arrayNumeros) {
        var resultado = 0;
        arrayNumeros.forEach(elemento => {
            resultado += elemento;
        });
        return resultado;
    }

    static chamaSomatorio(arrayNumeros){
        return this._somatorio(arrayNumeros);
    }
}

Obrigado !!!!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software