Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can't figrue out NaN issue

Posted on 2011-09-30
8
Medium Priority
?
151 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
  • 4
  • 4
8 Comments
 
LVL 83

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 83

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
Technology Partners: 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 83

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 83

Accepted Solution

by:
leakim971 earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

879 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