Solved

Can't figrue out NaN issue

Posted on 2011-09-30
8
148 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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

615 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