function returns Undefined array of strings

Asked

Viewed 48 times

0

I have been trying to show users balances in the following scenario, but I always come across these mistakes:

TypeError: Cannot read property 'lenght' of undefined
    at somaNumeros (C:\Users\leona\rockseat\ex05.js:26:24)
    at calculaSaldo (C:\Users\leona\rockseat\ex05.js:35:27)
    at Object.<anonymous> (C:\Users\leona\rockseat\ex05.js:45:24)
?[90m    at Module._compile (internal/modules/cjs/loader.js:1133:30)?[39m
?[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)?[39m
?[90m    at Module.load (internal/modules/cjs/loader.js:977:32)?[39m
?[90m    at Function.Module._load (internal/modules/cjs/loader.js:877:14)?[39m
?[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)?[39m
?[90m    at internal/main/run_main_module.js:18:47?[39m

I ask for the help of friends, thank you very much

const usuarios = [
    {
        nome: 'Salvio',
        receitas: [115.3, 48.7, 98.3, 14.5],
        despesas:[85.3, 13.5, 19.9]
    },
    {
        nome: 'Marcio',
        receitas: [24.6, 214.3, 45.3],
        despesas: [185.3, 21.1, 120.0]
    },
    {
        nome: 'Lucia',
        receitas: [9.8, 120.3, 340.2, 45.3],
        despesas: [450.2, 29.9]
    }


]



function somaNumeros(numeros)
{
    var soma = 0
    for(i=0; i< numeros.length; i++) 
    {
        soma = soma + numeros[i]
    }

    return soma
}
function calculaSaldo(receitas , despesas)
{
    const totalDespesas = somaNumeros(receitas)
    const totalReceitas = somaNumeros(despesas)
    return totalReceitas - totalDespesas
}




for(i=0;i<usuarios.length;i++)
{
    var saldototal = calculaSaldo(usuarios.receitas,usuarios.despesas)
    if(saldototal>0){
        console.log(`${usuarios.nome} possui saldo POSITIVO de ${saldototal}`)
    }else{
        console.log(`${usuarios.nome} possui saldo negativo de ${saldototal}`)
    }
}
  • Ash, the code you posted must be different from the one that generated the error, but note the following, the error says that TypeError: Cannot read property 'lenght', this property is actually length, you reversed the "th" at the end.

  • Javascript is not Java, please remove the tag Java...

1 answer

0


the error is simple,

but, first, I will try to show how to debug the code, especially in javascript, because just run the code in the browser:

https://www.dropbox.com/s/plpivwi52xdirhp/yo9AqxWq6h.mp4

it turned out to be simple because its loop for call the array, when only an Object should be called in the array:

for (i = 0; i < usuarios.length; i += 1)
{
   var usuario = usuarios[i];
   var saldototal = calculaSaldo(usuario.receitas, usuario.despesas);

   ...
}

Added by comment

the error of just showing one, has to do with "variable scoping" and is of simple resolution...

there are 2 loops for, with the same variable, so when adding 1 to the i of a for, it will remain as global variavle, and being called somaNumeros(), despite the setar with 0, that i will again be added, while flipping the loop for original, in the second interaction, will leave the loop because the condition i < usuarios.length has already been achieved.

that is, in other words, if the above text is confused: the variavle i used in both loops for as they are not initialized with var or let, is automatically as global variable.

add var variable i, to keep: for ( var i = 0; ... and you will see that all values will be shown.

your code should be:

var usuarios = [{
    nome: 'Salvio',
    receitas: [115.3, 48.7, 98.3, 14.5],
    despesas: [85.3, 13.5, 19.9]
  },
  {
    nome: 'Marcio',
    receitas: [24.6, 214.3, 45.3],
    despesas: [185.3, 21.1, 120.0]
  },
  {
    nome: 'Lucia',
    receitas: [9.8, 120.3, 340.2, 45.3],
    despesas: [450.2, 29.9]
  }
];

function somaNumeros(numeros) {
  var soma = 0;
  for (var i = 0; i < numeros.length; i += 1) soma += numeros[i];
  return soma;
}

function calculaSaldo(receitas, despesas) {
  const totalDespesas = somaNumeros(receitas);
  const totalReceitas = somaNumeros(despesas);
  return totalReceitas - totalDespesas;
}

(function () {
  for (var i = 0; i < usuarios.length; i += 1) {
    var usuario = usuarios[i];
    var saldototal = calculaSaldo(usuario.receitas, usuario.despesas);

    var tipoSaldo = saldototal > 0 ? 'POSITIVO' : 'negativo';
    console.log(
      `${usuario.nome} possui saldo ${tipoSaldo} de ${saldototal.toFixed(2)}`);
  }
})(); // funcão que corre automáticamente

  • thank you very much friend

  • i realized that only prints the first user after performing this change, how can I print all ?

  • added in the reply... if the answer helps, remember to mark as right

  • made friend, very grateful brother

Browser other questions tagged

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