I'm facing an issue with a NaN error in my TypeScript code. I've defined a variable type as number and loop through an element to retrieve various balance amounts. These values are in the form of "$..." such as $10.00 and $20.00, so I use a replace method and then add each balance amount to the total sum balance variable.
However, when I check my console log, it shows:
Expected: NaN
Actual: 20.00
I'm not sure why this is happening. Why does it consider it as not a number and how can I fix it (it should display 20.00)?
balance: Selector;
this.balance = Selector('.balance');
this.balanceTotal = Selector('.balance-total ');
async validateTotalBalance() {
let sumBalanceTotal: number = 0;
for (let i = 0; i < (await this.balance.count); i++) {
let amount = await this.balance.nth(i).textContent;
amount.replace('$', '');
let convertedAmount = Number(amount);
convertedAmount.toFixed(2);
sumBalanceTotal += convertedAmount;
}
console.log('Expected: ' + sumBalanceTotal);
console.log(
'Actual: ' + (await this.balanceTotal.textContent).replace('$', '')
);
}