Solved

Can't figrue out NaN issue

Posted on 2011-09-30
8
146 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
Independent Software Vendors: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Date on a table 16 34
Accordion won't close correctly 5 13
Two decimal 5 26
Keyup listener getting fired on initial page load 1 10
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
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-…
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…

756 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