You are viewing limited content. For full access, please sign in.

Question

Question

NaN Problem

asked on May 6, 2021

In a form I am creating I have the following table:

when I click the Date picker I get this:

here is the JS I am using:

$(document).ready(function () {
  
  $.getScript('http://server/Forms/js/moment.js');
  
  $('.cf-table-block').on('blur', 'input', sumtotal);
  $(document).on('click', '.form-del-field', sumtotal);
  
  function sumtotal() {
    var hour = 0;
    var rate = 0;
    var total = 0;
    var subtotal = 0;   
    $('.cf-table-block tbody tr').each(function () {
      hour = moment($(this).find('.hour input').val(), 'HHmm');
      rate = moment($(this).find('.rate input').val(), 'HHmm');
      subtotal = hour.diff(rate, 'hours', true);
      $(this).find('.total input').val(subtotal.toFixed(2));
      total += subtotal;
    });  
         
  }
  
  function parseNumber(n) { 
      var f = parseFloat(n); //Convert to float number. 
      return isNaN (f) ? 0 :f; //treat invalid input as 0; 
  } 

});

What am I doing wrong; How do I make the NaN not appear?

 

0 0

Replies

replied on May 6, 2021

It doesn't look like you're calling parseNumber() anywhere. The subtotal variable just gets set and then used based on what you're puling for hour and rate.

0 0
replied on May 6, 2021

Pieter,

Thank you for helping.  I should have said I was new to coding.  Can you give me an example of where I would put parseNumber() in the code?

0 0
replied on May 6, 2021

I think the easiest would be to use it in the subtotal calculation like

subtotal = parseNumber(hour.diff(rate, 'hours', true));

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.