Solved

Can't figrue out NaN issue

Posted on 2011-09-30
8
147 Views
Last Modified: 2012-06-21
I can't figrue out why I get an NaN no matter what I try, I'm sure I'm missing someting obvious, maybe just been working on this to long. I have "stripped down" the page that I'm generating via a coldfusion app to the "basics" this javascript works fine:

https://dev1.mystudentsprogress.com/test/testmath.html

As soon as I try to pass the results into some kind of function to format the total into a "currency" value I get a NaN no matter what I try, I"ve striped that functino way done to try and simply prepend a "$" , see javascript function FormatCurrency here:

https://dev1.mystudentsprogress.com/test/testmath1.html


0
Comment
Question by:Javier_Arroyo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36893637
replace :
  function calctotal(){
    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,6) == 'total_'){
        thisElement.value = 0;
      }
    }


    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,5) == 'item_'){
        var thisitem = thisElement.name.split('_');
        var thistot = 'total_' + thisitem[1] + '_' + thisitem[2];
        var totfield = 'document.lunchForm.'+thistot;
        thischarge = parseFloat(thisitem[4]) * parseFloat(thisElement.value);
        thistot = eval(totfield).value;
        tot = parseFloat(thistot) + parseFloat(thischarge);
        eval(totfield).value = FormatCurrency(tot);
      }
    }
  }

Open in new window

by :
  function calctotal(){
    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,6) == 'total_'){
        thisElement.value = 0;
      }
    }


    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,5) == 'item_'){
        var thisitem = thisElement.name.split('_');
        var thistot = 'total_' + thisitem[1] + '_' + thisitem[2];
        var totfield = 'document.lunchForm.'+thistot;
        thischarge = parseFloat(thisitem[4]) * parseFloat(thisElement.value);
        thistot = eval(totfield).value;
        tot = parseFloat(thistot.replace(/\D/g,"")) + parseFloat(thischarge.replace(/\D/g,""));
        eval(totfield).value = FormatCurrency(tot);
      }
    }
  }

Open in new window

0
 

Author Comment

by:Javier_Arroyo
ID: 36893696
leakim971 ...tried it getting an error:

Error: Object doesn't support property or method 'replace'

...repalced code here:

Error: Object doesn't support property or method 'replace'
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36893718
Try this one :
  function calctotal(){
    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,6) == 'total_'){
        thisElement.value = 0;
      }
    }


    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,5) == 'item_'){
        var thisitem = thisElement.name.split('_');
        var thistot = 'total_' + thisitem[1] + '_' + thisitem[2];
        var totfield = 'document.lunchForm.'+thistot;
        thischarge = parseFloat(thisitem[4]) * parseFloat(thisElement.value);
        thistot = eval(totfield).value;
        if(thistot.length==0||isNaN(thistot)) thistot = 0;
        if(thischarge.length==0||isNaN(thischarge)) thischarge = 0;
        tot = parseFloat(thistot.replace(/\D/g,"")) + parseFloat(thischarge.replace(/\D/g,""));
        eval(totfield).value = FormatCurrency(tot);
      }
    }
  }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Javier_Arroyo
ID: 36894162
Same merror :-(

...Oh and I see I dind't paste the replced code link!!:

http://dev1.mystudentsprogress.com/test/testmath2.html
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36894183
ok a last one :

  function calctotal(){
    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,6) == 'total_'){
        thisElement.value = 0;
      }
    }


    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,5) == 'item_'){
        var thisitem = thisElement.name.split('_');
        var thistot = 'total_' + thisitem[1] + '_' + thisitem[2];
        var totfield = 'document.lunchForm.'+thistot;
        thischarge = parseFloat(thisitem[4]) * parseFloat(thisElement.value);
        thistot = eval(totfield).value;
        if(thistot.length==0||isNaN(thistot)) thistot = 0;
        if(thischarge.length==0||isNaN(thischarge)) thischarge = 0;
        tot = parseFloat(thistot.replace(/\D/g,"")) + parseFloat(new String(thischarge).replace(/\D/g,""));
        eval(totfield).value = FormatCurrency(tot);
      }
    }
  }

Open in new window

0
 

Author Comment

by:Javier_Arroyo
ID: 36894271
Same error :-(

Line: 828
Error: Object doesn't support property or method 'replace'
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36894303
work for me : http://jsfiddle.net/K7nUC/3/
 
function calctotal(){

    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,6) == 'total_'){
        thisElement.value = 0;
      }
    }

    for( var i = 0; i < document.lunchForm.elements.length; i++){
      var thisElement = document.lunchForm.elements[i];
      if(thisElement.name.substr(0,5) == 'item_'){
        var thisitem = thisElement.name.split('_');
        var thistot = 'total_' + thisitem[1] + '_' + thisitem[2];
        var totfield = 'document.lunchForm.'+thistot;
        thischarge = parseFloat(thisitem[4]) * parseFloat(thisElement.value);
        thistot = eval(totfield).value;
        tot = parseFloat(thistot.replace("$","")) + parseFloat(thischarge);
        eval(totfield).value = FormatCurrency(tot);
      }
    }
  }

  function FormatCurrency(thisnum){
//    return '$' + thisnum;
    return '$' + thisnum.toString();
  }

Open in new window

0
 

Author Comment

by:Javier_Arroyo
ID: 36894331
I copied the javascript from your link above and now works great!!

http://dev1.mystudentsprogress.com/test/testmath2.html 
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question