...

View Full Version : error when using variables in a variable



tfair
02-27-2007, 04:56 PM
Newbie here......I have been trying to learn JavaScript for about two weeks now from online sources. I am trying to create a "calculator" but I am havinf trouble in the first few calculations. I have a form that I am pulling the initial variables from and that works great. But as soon as I use a crreated variable to calculate another variable it returns NaN. I have tried to parseInt(var) and several ways to call it including by"id" and by "Name" and by ".value" and by including a separate function inside the function. Please let me know where I am missing a huge conceptual Idea here.

the error comes in the second line where I use the var I just created in the previous line. It returns as Not a Number






var cpadjustedincomeno= parseInt(cpgrossincome.value) - parseInt(cpdeductions.value)
var cpadjustedincome= parseInt(cpadjustedincomeno.value) - parseInt(cpothercs.value)




link to working test is "http://www.bryantpsc.com/csform.htm"

Philip M
02-27-2007, 06:58 PM
You don't need .value if the variable is already defined. E.g.:

<SCRIPT>
var cpgrossincome=10;
var cpdeductions=3;
var cpothercs=2;

var cpadjustedincomeno= parseInt(cpgrossincome) - parseInt(cpdeductions);
alert (cpadjustedincomeno);
var cpadjustedincome= parseInt(cpadjustedincomeno) - parseInt(cpothercs);
alert (cpadjustedincome);

</SCRIPT>

tfair
02-27-2007, 08:25 PM
Maybe I did not post enough of the code but that also causes an error. That is why I went ahead and used the.value to make sure I was pointing to the value. It is because I get the NaN that I could not figure out what is going on. I have incorporated the suggested code (I started with something very similar) but I still get a NaN error when I plug it in. That is what is driving me crazy is this should not be that hard and should be very straight forward, but I have obviously missed something.

I have tried several different ways but I can not get around the NaN error. does it have something to do with the way the variable is defined?

I have posted everything including the form. It is a child support calculator in the very beginning stages. I can not even get the percentage of the adjusted income to come out right, so I have not completed the script




<head>
<script type="text/javascript">


function cscalc(cpadin) {

var cpgrossincome= document.getElementById('cpgrossincome');
var ncpgrossincome= document.getElementById('ncpgrossincome');
var cpdeductions= document.getElementById('cpdeductions');
var ncpdeductions= document.getElementById('ncpdeductions');
var cpothercs= document.getElementById('cpothercs');
var ncpothercs= document.getElementById('ncpothercs');


var cpadjustedincomeno= parseInt(cpgrossincome) - parseInt(cpdeductions);
alert (cpadjustedincomeno);
var cpadjustedincome= parseInt(cpadjustedincomeno) - parseInt(cpothercs);
alert (cpadjustedincome);var ncpadjustedincome= (parseInt(ncpgrossincome.value) - parseInt(ncpdeductions.value))-parseInt(ncpothercs.value)

var combinedincome= parseInt(ncpadjustedincome.value) + parseInt(cpadjustedincome.value)
var ncppercentcombined= (parseInt (ncpadjustedincome.value) + parseInt(cpadjustedincome.value)) / parseInt(ncpadjustedincome.value)

var cppercentcombinedincome = ((cpadjustedincome / combinedincome)*100)
var ncppercentcombinedincome = (parseInt(ncpadjustedincome) / parseInt(combinedincome))*100

alert("The custodial gross income is:" + cpgrossincome)
alert("the custodial adjusted income is :" + cpadjustedincome)
alert("the non-custodial parent combined percentage is:" + ncppercentcombinedincome)}


function cpadjin(cpadjustedincomeno, cpothercs){

var cpadjustedincome= parseInt(cpadjustedincomeno) - parseInt(cpothercs)
return cpadjustedincome}



</script>

</head>

<body>


<form name="csform" action="#"><table align="center"
border="1">

<tr><td align="left"></td>
<td align="left">Custodial Parent</td>
<td align="left">Non Custodial Parent</td>
<td align="left"></tr>

<tr><td align="right">Monthly Income: </td>
<td align="left"><input type="text" id="cpgrossincome" value="0" size="5" />
</td><td align="left"><input type="text" id="ncpgrossincome" value="0" size="5" />
</td></tr>

<tr><td align="right">Monthly Deductions: </td>
<td align="left"><input type="text" id="cpdeductions" value="0" size="5" />
</td><td align="left"><input type="text" id="ncpdeductions" value="0" size="5" />
</td></tr>

<tr><td align="right">Monthly Deductions for additional child support received for prior children: </td>
<td align="left"><input type="text" id="cpothercs" value="0" size="5" />
</td><td align="left"><input type="text" id="ncpothercs" value="0" size="5" />
</td></tr>

<tr><td align="right">Number of children with this parent: </td>

<td colspan="2" rowspan="1" valign="top">
<id="radios">
<label><input type="radio" name="numchild" "value="child1" > 1 Child</label><br />
<label><input type="radio" name="numchild" "value="child2" > 2 Children</label><br />
<label><input type="radio" name="numchild" "value="child3" > 3 Children</label><br />
<label><input type="radio" name="numchild" "value="child4" > 4 Children</label><br />
<label><input type="radio" name="numchild" "value="child5" > 5 Children</label><br />
<label><input type="radio" name="numchild" "value="child6" > 6 or more Children</label>
</td>
</tr>




<tr></tr>
<tr></tr>
<tr><td colspan="4" align="center">
<input type="button" value="Calculate"
onclick="cscalc();" />

<tr><td align="right">custodial parent percent of combined monthly income:</td>
<td alig="left"><input type="text" value=cppercentcombinedincome size="5"
readonly="readonly" /></td></tr><tr>
<td align="right">non-custodial parent percent of combined monthly income:</td><td alig="left">
<input type="text" value=ncppercentcombinedincome size="6"
readonly="readonly" /></td></tr>
</table></form>



</body>

glenngv
02-27-2007, 09:01 PM
<html>
<head>
<script type="text/javascript">

function cscalc() {

var cpgrossincome= document.getElementById('cpgrossincome');
var ncpgrossincome= document.getElementById('ncpgrossincome');
var cpdeductions= document.getElementById('cpdeductions');
var ncpdeductions= document.getElementById('ncpdeductions');
var cpothercs= document.getElementById('cpothercs');
var ncpothercs= document.getElementById('ncpothercs');


var cpadjustedincomeno= parseFloat(cpgrossincome.value) - parseFloat(cpdeductions.value);
alert (cpadjustedincomeno);
var cpadjustedincome= cpadjustedincomeno - parseFloat(cpothercs.value);
alert (cpadjustedincome);
var ncpadjustedincome= parseFloat(ncpgrossincome.value) - parseFloat(ncpdeductions.value) - parseFloat(ncpothercs.value);

var combinedincome= ncpadjustedincome + cpadjustedincome;
var ncppercentcombined= (ncpadjustedincome + cpadjustedincome) / ncpadjustedincome;

var cppercentcombinedincome = (cpadjustedincome / combinedincome)*100;
var ncppercentcombinedincome = (ncpadjustedincome / combinedincome)*100;

document.getElementById("cppci").value = cppercentcombinedincome.toFixed(2); //2 decimal places
document.getElementById("ncppci").value = ncppercentcombinedincome.toFixed(2);

alert("The custodial gross income is:" + cpgrossincome.value);
alert("the custodial adjusted income is :" + cpadjustedincome.toFixed(2));
alert("the non-custodial parent combined percentage is:" + ncppercentcombinedincome.toFixed(2));
}

</script>

</head>

<body>


<form name="csform" action="#"><table align="center"
border="1">

<tr><td align="left"></td>
<td align="left">Custodial Parent</td>
<td align="left">Non Custodial Parent</td>
<td align="left"></tr>

<tr><td align="right">Monthly Income: </td>
<td align="left"><input type="text" id="cpgrossincome" value="0" size="5" />
</td><td align="left"><input type="text" id="ncpgrossincome" value="0" size="5" />
</td></tr>

<tr><td align="right">Monthly Deductions: </td>
<td align="left"><input type="text" id="cpdeductions" value="0" size="5" />
</td><td align="left"><input type="text" id="ncpdeductions" value="0" size="5" />
</td></tr>

<tr><td align="right">Monthly Deductions for additional child support received for prior children: </td>
<td align="left"><input type="text" id="cpothercs" value="0" size="5" />
</td><td align="left"><input type="text" id="ncpothercs" value="0" size="5" />
</td></tr>

<tr><td align="right">Number of children with this parent: </td>

<td colspan="2" rowspan="1" valign="top">
<id="radios">
<label><input type="radio" name="numchild" "value="child1" > 1 Child</label><br />
<label><input type="radio" name="numchild" "value="child2" > 2 Children</label><br />
<label><input type="radio" name="numchild" "value="child3" > 3 Children</label><br />
<label><input type="radio" name="numchild" "value="child4" > 4 Children</label><br />
<label><input type="radio" name="numchild" "value="child5" > 5 Children</label><br />
<label><input type="radio" name="numchild" "value="child6" > 6 or more Children</label>
</td>
</tr>
<tr></tr>
<tr></tr>
<tr><td colspan="4" align="center">
<input type="button" value="Calculate"
onclick="cscalc();" />

<tr><td align="right">custodial parent percent of combined monthly income:</td>
<td alig="left"><input type="text" id="cppci" value="0" size="5"
readonly="readonly" /></td></tr><tr>
<td align="right">non-custodial parent percent of combined monthly income:</td><td alig="left">
<input type="text" id="ncppci" value="0" size="6"
readonly="readonly" /></td></tr>
</table></form>
</body>
</html>

tfair
02-27-2007, 09:24 PM
glenngv: Never though of using the parseFloat to identify the number, it makes sense to parse the number. Only I still do not understand why the value continues to return as "NaN". Even after parsing the value of the variable still returns as not a number. O.K. Now I am totally confused where is this getting anything but numbers from?



I also see where you set the input of the two lower fields to be input for "cppci" and "ncppci". That was my mistake for not stating earlier that I was going to try to make those fields report the values calculated in the function above, but since I could not get the function to work I never corrected the input. Thanks for noticing that.

glenngv
02-27-2007, 09:44 PM
NaN only shows if the inputs remain as 0 because 0/0 returns a NaN value. There must be a check if the divisor is 0, division should not be done.

Change this part:

var ncppercentcombined = (ncpadjustedincome + cpadjustedincome) / ncpadjustedincome;

var cppercentcombinedincome = (cpadjustedincome / combinedincome)*100;
var ncppercentcombinedincome = (ncpadjustedincome / combinedincome)*100;to this:

var ncppercentcombined= (ncpadjustedincome==0) ? 0 : (ncpadjustedincome + cpadjustedincome) / ncpadjustedincome;

var cppercentcombinedincome = 0;
var ncppercentcombinedincome = 0;
if (combinedincome>0) {
cppercentcombinedincome = (cpadjustedincome / combinedincome)*100;
ncppercentcombinedincome = (ncpadjustedincome / combinedincome)*100;
}

tfair
02-27-2007, 10:38 PM
agreed. I should have a check for non zero numbers but the line that returns NaN is before the division it is the line:



var cpadjustedincome= (cpgrossincome-cpdeductions)-cpothercs

the var cpadjustedincome returns as a NaN error.......how?

I even broke the math down to what I posted before so the subtraction was done one line at a time and the "-cpothercs" was moved to it's own line. It still came back cpadjustedincome as a NaN error



var cpadjustedincomeno= cpgrossincome-cpdeductions;
var cpadjustedincome= cpadjustedincomeno-cpothercs;



I wanted to get the code to work with real numbers then go in and start to verify so I knew the code worked before verification broke it.


here is the original code that I wrote before adding everything under the sun to try and fix the NaN error.



<script type="text/javascript">


function cscalc() {

var cpgrossincome= document.getElementById('cpgrossincome');
var ncpgrossincome= document.getElementById('ncpgrossincome');
var cpdeductions= document.getElementById('cpdeductions');
var ncpdeductions= document.getElementById('ncpdeductions');
var cpothercs= document.getElementById('cpothercs');
var ncpothercs= document.getElementById('ncpothercs');
var cpadjustedincome= (cpgrossincome-cpdeductions)-cpothercs
var ncpadjustedincome= (ncpgrossincome-ncpdeductions)-ncpothercs
var combinedincome= ncpadjustedincome+cpadjustedincome
var ncppercentcombined= (ncpadjustedincome+cpadjustedincome) / ncpadjustedincome
var cppercentcombinedincome = (cpadjustedincome / combinedincome)*100
var ncppercentcombinedincome = (ncpadjustedincome / combinedincome)*100
alert("The custodial gross income is:" + cpgrossincome)
alert("the custodial adjusted income is :" + cpadjustedincome)
alert("the non-custodial parent combined percentage is:" + ncppercentcombinedincome)}


</script>

glenngv
02-28-2007, 02:38 AM
Did you try the whole code that I gave you? I didn't get any NaN error anymore after I fixed that division error.

tfair
02-28-2007, 03:06 PM
glenngv:

Thanks for sticking with me on this. I saw a small error (I put the 0 div code in the test script without the .value) as soon as I added the 0 div code to the working example the values came out right......THANK YOU:thumbsup:

I will now attempt to tackle arguments and if then statements to pull a number out of a chart based on the combined income result. Again thanks.


P.S. I add to rep but that still does not do your service justice.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum