Why is the Sumary function returning Undefined?

Asked

Viewed 117 times

-1

When the function console.log(contas.sumario()) class CicloFinanceiro is called, appears the two objects as if expected and then appears Undefined and do not understand why.

class Lancamento { constructor(nome = 'Genérico', valor = 0){ this.nome = nome this.valor = valor } }

Função que retorna undefined

  • 4

    Don’t post code as image, it’s bad to read and even worse to try to simulate your problem

  • 3

    The method .forEach always returns undefined. What was the result you hoped to achieve? You may want to use another method but only knowing what you expect we can help you more.

1 answer

2


Returns undefined because you are asking for the method sumario() return of array method forEach, and this method by default executes a callback function for each element of the array and always returns the value of undefined.

You can see it through this Developer Mozilla link, where he says the following:

'' forEach() executes the callback function once for each element of the array - unlike map() or reduce(), it always returns the value undefined and is not chained. The typical use case is to change the array at the end of the loop. ''.

If you don’t want to see the undefined, rewrite: console.log(conta.sumario()) thus conta.sumario();

Browser other questions tagged

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