Balance calculation (Credit - Debit) resulting in Nan

Asked

Viewed 37 times

-1

I’m a rookie and I’m learning launchbase’s Ootcamp. By making a JS that calculates credit - debits, is giving me Nan, although Typeof is saying it’s a number...

Can help with what I did wrong?

const user = 
{
    name: "Mariana",
    transaction:[],
    balance: 0,
    credit: [50, 120],
    debit: [80, 30],
};

function sumCredit(credit)
{
    const sumCredit = sumNumbers(credit)

    return sumCredit
}

function sumDebit(debit)
{
    const sumDebit = sumNumbers(debit)

    return sumDebit
}

function sumBalance(credit, debit)
{

    return sumCredit - sumDebit
}

function sumNumbers(numbers)
{
        let sum = 0;
        for (let number of numbers)
        {
            sum = sum + number
        }
        return sum 
}

const balanceCredit = sumCredit(user.credit)
const balanceDebit = sumDebit(user.debit)
const balanceTotal = sumBalance(user.credit, user.debit)

console.log(balanceCredit.toFixed(2))
console.log(balanceDebit.toFixed(2))

console.log(balanceTotal)

  • return sumCredit - sumDebit is the subtraction of functions, you are not invoking them to subtract the results

2 answers

0


You need to keep your code simple, the concept of creating functions serves exactly so that you divide your code into small pieces that can be used in a generic way in various parts of your program, for example: Its functions sumCredit and sumDebit do exactly the same thing, invoke a method to do the sum of the value passed and return such value, ie do nothing, so you can for example delete them and directly call the function that executes the sum of the values passed.

To make the sum you can use the reduce that applied in an array will perform the required operation according to the function passed as parameter. See below an example of your code as could be minified and yet more readable and simple.

const user = 
{
    name: "Mariana",
    transaction:[],
    balance: 0,
    credit: [50, 120],
    debit: [80, 30],
};

function sum(values){
    return values.reduce((a, b) => a + b);
}

const balanceCredit = sum(user.credit);
const balanceDebit = sum(user.debit);
const balanceTotal = balanceCredit - balanceDebit;

console.log({balanceCredit});
console.log({balanceDebit});

console.log({balanceTotal});

The same happens with the function sumBalance that adds up the values and returns the difference between them, there is no need because once you have calculated the sum of the debts and credits, you can directly make the difference without the need to create a function to do this operation by you.

  • I agree that the code in this way becomes more elegant, but it may be the case that it becomes more difficult to change if you have some forward change, like the way the data arrives, for example.

  • 1

    From the statement this seems to be only an exercise, so I did not worry about such problems. If one day it became more complex, I believe that the correct thing would be to create a class User with the necessary methods inside instead of a simple json.

  • Both of you are correct! It is an exercise but I already want to have 2 functions (Debit and Credit) where, when changing the debit function, is already reflected in the balance function. Leo has provided a clear answer that solves Nan’s problem, and I thank you for that! I will learn more about the reduce function, which at the time of the course was not presented to me.

-1

There are two errors in your code. First in function sumBalance you’re subtracting sumCredit and sumDebit when you would have to subtract the variables you passed to function credit and debit.

function sumBalance(credit, debit) {
    return credit - debit
}

Second mistake is when you’re calling sumBalance. You are passing the arrays that are inside the user instead of passing the aggregates you added. The correct one would be sumBalance(sumCredit, sumDebit).

What helps you find errors in this type of code is to test each component individually. I didn’t find the bugs right away, I tested each of their functions and saw that sumBalance wasn’t returning what I expected. After correcting the sumBalance was still not working, but as the functions were correct I went to see if there was any error in the part calling the functions.

  • Still with error! I made each component, as you mention, but it continues with Nan or else gives error. Veja a parte que editei:
function sumBalance(sumCredit, sumDebit)
{
 const sumBalance = sumCredit - sumDebit

 return sumBalance
}

const balanceTotal = sumBalance(user.sumCredit, user.sumDebit)

  • I edited the answer to make the change in sumBalance clearer

Browser other questions tagged

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