Solved

Can't figrue out NaN issue

Posted on 2011-09-30
8
143 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 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now