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 actuallylength
, you reversed the "th" at the end.– Daniel Mendes
Javascript is not Java, please remove the tag
Java
...– balexandre