2

I have this code, but I want to add it in case if the value in the price field is equal to a string, how can this value be discarded and not counted and sum the rest of the values of type number

var theSummry = 0;
$(".price").each(function() {
  theSummry += parseInt($(this).text());
})
$(".the-total").text(theSummry);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table style="width:70%;text-align: center;margin: 30px auto;">
  <tr>
    <th>Person 1</th>
    <th>Person 2</th>
    <th>Person 3</th>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td>Linus</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">10</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">qwe</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">10</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">30</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">10</td>
  </tr>
  <tr class="the-total"></tr>
</table>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
haroun
  • 110
  • 1
  • 1
  • 12
  • Check [`$(this).text().isInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger) in the loop, and if so parse it as an integer. Otherwise, either skip it ([using `return;`](https://stackoverflow.com/questions/17162334/how-to-use-continue-in-jquery-each-loop)) or add 0 instead. – WOUNDEDStevenJones Nov 29 '21 at 16:53
  • 1
    @WOUNDEDStevenJones `$(this).text()` always returns a string, so you can't call `isInteger()` on it in this case. – Rory McCrossan Nov 29 '21 at 16:55
  • That's what I get for reading the docs too quickly... Also the format I listed above isn't valid. This is what I intended: `Number.isInteger(parseInt('test'));` returns false, and `Number.isInteger(parseInt('123'));` returns true. Which isn't far off from the accepted answer. – WOUNDEDStevenJones Nov 29 '21 at 17:40

1 Answers1

1

To achieve this you can coalesce the NaN output from trying to parseInt() a string to a 0 value using the logical OR operator:

var theSummry = 0;
$(".price").each(function() {
  theSummry += parseInt($(this).text(), 10) || 0;
})
$(".the-total").text(theSummry);
table {
  width: 70%;
  text-align: center;
  margin: 10px auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table>
  <tr>
    <th>Person 1</th>
    <th>Person 2</th>
    <th>Person 3</th>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td>Linus</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">10</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">qwe</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">10</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">30</td>
  </tr>
  <tr>
    <td>Emil</td>
    <td>Tobias</td>
    <td class="price">10</td>
  </tr>
  <tr class="the-total"></tr>
</table>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339