Click to See Complete Forum and Search --> : Calculator Woes


DC_Kelly
September 28th, 2005, 01:39 AM
Hey, I am sorta new here and in need of help. I have course in javascript for college, and before this course I had never looked at javascript before. Unfortunately for me, and the rest of my class for that matter, my teacher doesn't really teach. He shows us what he wants, and then we have to figure out how to do that. Which is sort of redundant because I am paying 10k a year to learn, not surf the net trying to teach something to myself. I have e-mailed him with hopes of setting up some time for me to try and learn it one on one, however until that is set up I have no other method of learning aside from somewhere like this. Anyways, I am having some calculator woes. I have been running into the following error"

Script Error (Code 0):

Line 20, Column 1

'FKeyPad.current_num' is null or not an object

I understand that the error is on line twenty, however I don't understand what it is trying to say, as I said, I am not fimilar with javascript errors and my teacher has failed to intstruct us on what to do if this error were to appear. This is my whole script. It is largely based off a tutorial I had found, as it was the closest learning tool I had. I am sorry the code is so long, I just need help, and am so lost I don't know where to start.



<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var FKeyPad = document.Keypad;
var Accum = 0;
var FlagNewNum = false;
var PendingOp = "";

function NumPressed (Num) {
if (FlagNewNum) {
FKeyPad.ReadOut.value = Num;
FlagNewNum = false;
}
else {
if (FKeyPad.current_num.value == "0")
FKeyPad.current_num.value = Num;
else
FKeyPad.current_num.value += Num;
}
}
function Operation (Op) {
var ReadOut = FKeyPad.ReadOut.value;
if (FlagNewNum && PendingOp != "=");
else
{
FlagNewNum = true;
if ( '+' == PendingOp )
Accum += parseFloat(Readout);
else if ( '-' == PendingOp )
Accum -= parseFloat(Readout);
else if ( '/' == PendingOp )
Accum /= parseFloat(Readout);
else if ( '*' == PendingOp )
Accum *= parseFloat(Readout);
else
Accum = parseFloat(Readout);
FKeyPad.ReadOut.value = Accum;
PendingOp = Op;
}
}
function Decimal () {
var curReadOut = FKeyPad.ReadOut.value;
if (FlagNewNum) {
curReadOut = "0.";
FlagNewNum = false;
}
else
{
if (curReadOut.indexOf(".") == -1)
curReadOut += ".";
}
FKeyPad.ReadOut.value = curReadOut;
}
function ClearEntry () {
FKeyPad.ReadOut.value = "0";
FlagNewNum = true;
}
function Clear () {
Accum = 0;
PendingOp = "";
ClearEntry();
}
function Neg () {
FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * -1;
}
function Percent () {
FKeyPad.ReadOut.value = (parseFloat(FKeyPad.ReadOut.value) / 100) * parseFloat(Accum);
}
// End -->
</SCRIPT>

</head>

<body>
<form action="./index.html" method="post" name="KeyPad">
<table width="200" border="1" align="center">
<tr>
<td colspan="4">
<div align="center">My Calculator </div></td>
</tr>
<tr>
<td colspan="4">
<div align="center">
<input name="current_num" type="text" id="current_num" size="20" value="">
</div></td>
</tr>
<tr>
<td colspan="4">
<div align="center">
<input name="ReadOut" type="text" id="Readout" size="20">
</div></td>
</tr>
<tr>
<td><input name="but7" type="button" value=" 7 " onclick="NumPressed(7)"></td>
<td><input name="but8" type="button" value=" 8 " onclick="NumPressed(8)"></td>
<td><input name="but9" type="button" value=" 9 " onclick="NumPressed(9)"></td>
<td><input name="butp" type="button" value=" + " onclick="Operation('+')"></td>
</tr>
<tr>
<td><input name="put4" type="button" value=" 4 " onclick="NumPressed(7)"></td>
<td><input name="but5" type="button" value=" 5 " onclick="NumPressed(7)"></td>
<td><input name="but6" type="button" value=" 6 " onclick="NumPressed(6)"></td>
<td><input name="buts" type="button" value=" - " onclick="Operation('-')"></td>
</tr>
<tr>
<td><input name="but1" type="button" value=" 1 " onclick="NumPressed(1)"></td>
<td><input name="but2" type="button" value=" 2 " onclick="NumPressed(2)"></td>
<td><input name="but3" type="button" value=" 3 " onclick="NumPressed(3)"></td>
<td><input name="butm" type="button" value=" * " onclick="Operation('*')"></td>
</tr>
<tr>
<td><input name="butd" type="button" value=" . " onclick="Decimal()"></td>
<td><input name="but0" type="button" value=" 0 " onclick="NumPressed(0)"></td>
<td><input name="butd" type="button" value=" / " onclick="Operation('/')"></td>
<td><input name="bute" type="button" value=" = " onclick="Operation('=')"></td>
</tr>
</table>
</form>

<p>&nbsp;</p>
</body>

olivthill
September 28th, 2005, 06:08 AM
Welcome DC_Kelly,

Many other students are posting questions here, and, oddly enough, many people like giving them answers, maybe it's making them feel important, lol.

A typical mistake is to forget to convert strings to numbers with NUmber(), and I suppose that's what happened in your code. For instance, instead of FKeyPad.current_num.value += Num; you could write FKeyPad.current_num.value += Number(Num);, or instead of var curReadOut = FKeyPad.ReadOut.value;, write var curReadOut = Number(FKeyPad.ReadOut.value);

Hoping it helps.

Comike14
September 28th, 2005, 10:24 AM
Been there before, man.
Anyway, the error you get is typically caused by using a variable or object that you never defined with "var." Now it looks like you may be using some JavaScript predefined object with "document.Keypad." I don't know because I've never used it. But let's assume that you are. You're using that "document.Keypad" to copy itself into a new object, which you call "FKeypad." So now this FKeypad is the same as document.Keypad.
I never see you explicitly say something like "FKeypad.current_num = 5" or anything, so I can assume that you, or the original creators of the code think that FKeypad.current_num is a data member of the original document.Keypad object. In other words, document.Keypad.current_num must already exist for you to make use of your code.
If this code is from some example you found, I would go back to where you got that example and make sure document.Keypad.current_num wasn't passed into this function as a parameter from somewhere else. It could also be that this code was written back in a different version of JavaScript that no longer supports this particular object, or the data members of this object may have changed.
I would suggest you google "document.Keypad javascript" or something like that to find out as much as you can about that object. If this post makes no sense to you, and you not only aren't familiar with JavaScript, but also are new to programming, I can try to clarify what I said. Good luck.