Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
Why i have "Nan" value from the loop which is summing up the table column

I have a problem with this loop. I want to sum up the values from the table, but i have NaN value from this code. Should i change something or is there any other solution to sum up?

function sumUpWallet() {
    var walletTable = document.getElementById("usersCrypto");
    let sumVal = 0;
    for (var i = 1; i < (walletTable.rows.length - 2); i++) {
        const tableData = walletTable.rows[i].cells[3];
            if (tableData && tableData.textContent) {
                const value = parseFloat(tableData.textContent);
                if (!isNaN(value)) {
                sumVal = sumVal + value;
                };
            };
    }
    console.log(sumVal);
    document.getElementById("sumDol").innerHTML = sumVal.toFixed(2);
};

Table is dynamic, after click the button another row is added. For example it can looks like this:

Screenshot page

Example table accesibility

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Problem is when you do:

console.log(walletTable.rows[i].cells[3]);

you get the follow result back <td>19271.71</td> instead of the figure 19271.71 for the first result of i

To fix this do add .textContent to the parseFloat like below

sumVal = sumVal + parseFloat(walletTable.rows[i].cells[3].textContent);

Added:

It looks like you are accessing data outside the range of the table. You need to do some validation to ensure what you are reading is valid before adding it to sumVal.

const tableData = walletTable.rows[i].cells[3];
if (tableData && tableData.textContent) {
    const value = parseFloat(tableData.textContent);
    if (!isNaN(value)) {
       sumVal = sumVal + value;
    }
}

Added an example that proves the above code works.

function sumUpWallet() {
    var walletTable = document.getElementById("usersCrypto");
    let sumVal = 0;
    for (var i = 1; i < (walletTable.rows.length - 2); i++) {
        const tableData = walletTable.rows[i].cells[3];
            if (tableData && tableData.textContent) {
                const value = parseFloat(tableData.textContent);
                if (!isNaN(value)) {
                sumVal = sumVal + value;
                };
            };
    }
    console.log(sumVal);
    document.getElementById("sumDol").innerHTML = sumVal.toFixed(2);
};

sumUpWallet();
<table id="usersCrypto" border="1">
  <tr>
    <td>
        Name
    </td>
    <td>
        price
    </td>
    <td>
        Amount
    </td>
    <td>
        Header
    </td>
   <tr>
   
   
   <tr>
    <td>
        Bitcoin
    </td>
    <td>
        19271.71 
    </td>
    <td>
        1
    </td>
    <td>
        19271.71 
    </td>
   <tr>
   
   <tr>
    <td>
        Ethereum
    </td>
    <td>
        1070.11 
    </td>
    <td>
        2
    </td>
    <td>
        2140.23
    </td>
   <tr>
   
   <tr>
    <td colspan="2">
        
    </td>
    <td>
        PLN
    </td>
    <td>
        DOL$
    </td>
   <tr>
   
   <tr>
    <td colspan="2">
       Summary Value 
    </td>
    <td>
        
    </td>
    <td id="sumDol">
        
    </td>
   <tr>
</table>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda