473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

learning JavaScript - Need help with an exercise

Hi I am doing a JavaScript course and learning from the recommed book
(JavaScript 3rd Edition by Don Gosslin)

Below is one of the exercises from the book. I get this error message
when I try to use the calculator.

"document.Calculate.Input is null or not an object"

Can someone please tell me why this is?

I have checked and double checked that my code is exactly the same as
the book.
Thanks


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Calculator exercise P120</title>
<META NAME="Generator" CONTENT="Stone's WebWriter 3.5">
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
<!-- hide from incompatible browsers

/*
the function is used to update the inputString variable above
x += y is the same as x = x + y
the variable named inputString contains the operands and operators of
the calculation
after the calculation is added to the input string variable, the
calculation is performed using the eval() function
the function updateString() accepts a single value representing a
number or operator
the value is then added to the inputString variable using the +=
assignment operator
after the inputString variable is updated, it is assigned the value of
a text box in the form

go to validator.w3.org/file-upload.html to validate this document

*/

var inputString = "";
function updateString(value)
{
inputString += value;
document.Calculator.Input.value = inputString;
}

// Stop hiding from incompatible browsers -->
</script>
</head>

<body>

<form name="Calculator" action =""> <!-- adds a form element -->
<input type="text name="Input" /><br /><br /> <!-- adds an input text
box -->

<!-- // each element along with the other elements sends a value to
the updateString() function using an onclick method -->

<input type="button" name="plus" value=" + " onclick="updateString(' +
')" />
<input type="button" name="minus" value=" - " onclick="updateString('
- ')" />
<input type="button" name="times" value=" x " onclick="updateString('
* ')" />
<input type="button" name="div" value=" / " onclick="updateString(' /
')" />
<input type="button" name="mod" value=" mod " onclick="updateString('
% ')" /><br />
<input type="button" name="zero" value=" 0 " onclick="updateString(' 0
')" />
<input type="button" name="one" value=" 1 " onclick="updateString(' 1
')" />
<input type="button" name="two" value=" 2 " onclick="updateString(' 2
')" />
<input type="button" name="three" value=" 3 " onclick="updateString('
3 ')" />
<input type="button" name="four" value=" 4 " onclick="updateString(' 4
')" /><br />
<input type="button" name="five" value=" 5 " onclick="updateString(' 5
')" />
<input type="button" name="six" value=" 6 " onclick="updateString(' 6
')" />
<input type="button" name="seven" value=" 7 " onclick="updateString('
7 ')" />
<input type="button" name="eight" value=" 8 " onclick="updateString('
8 ')" />
<input type="button" name="nine" value=" 9 " onclick="updateString(' 9
')" />
<br />

<!-- // the onclick event for the calc button performs the calculation
using the eval() function with the input string variable -->
<!-- // the calculated value is then assigned as the value of the
input text box -->

<input type="button" name="point" value=" . " onclick="updateString('
.. ')" />
<input type="button" name="clear" value=" clear "
onclick="document.Calculator.Input.value=''; inputString=''" />
<input type="button" name="calc" value=" = "
onclick="document.Calculator.Input.value=eval(inpu tString);
inputString=''" />

</form>

</body>

</html>
Jul 23 '05 #1
3 2538
Sean McCourt wrote:
Hi I am doing a JavaScript course and learning from the recommed book
(JavaScript 3rd Edition by Don Gosslin)
If the code you have posted is representative of what the book teaches
you, you need a new book.
Below is one of the exercises from the book. I get this error message
when I try to use the calculator.

"document.Calculate.Input is null or not an object"

Can someone please tell me why this is?
Yes.
I have checked and double checked that my code is exactly the same as
the book.
You missed the one thing killing it, that validating the HTML would have
caught.
<--snip-->

go to validator.w3.org/file-upload.html to validate this document
From your own code, had you done that it would have pointed out the
problem.

<--snip-->
<input type="text name="Input" /><br /><br /> <!-- adds an input text


type="text" note the closing " after the word text that you are missing.

<--snip-->

That is ignoring the <!-- //--> that are *not* needed in modern
browsers. It also ignores the eval usage, although it could be argued
that the use given is an "adequate" use of eval, there are better
methods to creating a JS driven calculator.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #2
Sean McCourt wrote:
Hi I am doing a JavaScript course and learning from the recommed book
(JavaScript 3rd Edition by Don Gosslin)

Below is one of the exercises from the book. I get this error message
when I try to use the calculator.

"document.Calculate.Input is null or not an object"

Can someone please tell me why this is?

I have checked and double checked that my code is exactly the same as
the book.
Thanks


I note Randy has replied with a solution, however since we're in the
same boat (but I'm a little ahead of you learning javascript) I find
the Mozilla web browser handy for helping me solve problems as it has a
Javascript console that gives you better error messages... thus, I
suggest you go to http://www.mozilla.org and near the lower right
corner, you'll see a link for Mozilla 1.7.5... Download it and use it as
your browser... If you want to see any errors in your codeing, select
Tools, Web Development, Javascript Console from the menu and it'll save
you posting here and waiting for replies...

Best of luck... you'll reach a few bumps along the way but don't be put
off - javascript is rather useful for your skillset...

randelld
Jul 23 '05 #3
Thank you both for your help

On Mon, 14 Mar 2005 14:15:25 +1100, Sean McCourt <sm*@ihug.com.au>
wrote:
Hi I am doing a JavaScript course and learning from the recommed book
(JavaScript 3rd Edition by Don Gosslin)

Below is one of the exercises from the book. I get this error message
when I try to use the calculator.

"document.Calculate.Input is null or not an object"

Can someone please tell me why this is?

I have checked and double checked that my code is exactly the same as
the book.
Thanks


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Calculator exercise P120</title>
<META NAME="Generator" CONTENT="Stone's WebWriter 3.5">
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
<!-- hide from incompatible browsers

/*
the function is used to update the inputString variable above
x += y is the same as x = x + y
the variable named inputString contains the operands and operators of
the calculation
after the calculation is added to the input string variable, the
calculation is performed using the eval() function
the function updateString() accepts a single value representing a
number or operator
the value is then added to the inputString variable using the +=
assignment operator
after the inputString variable is updated, it is assigned the value of
a text box in the form

go to validator.w3.org/file-upload.html to validate this document

*/

var inputString = "";
function updateString(value)
{
inputString += value;
document.Calculator.Input.value = inputString;
}

// Stop hiding from incompatible browsers -->
</script>
</head>

<body>

<form name="Calculator" action =""> <!-- adds a form element -->
<input type="text name="Input" /><br /><br /> <!-- adds an input text
box -->

<!-- // each element along with the other elements sends a value to
the updateString() function using an onclick method -->

<input type="button" name="plus" value=" + " onclick="updateString(' +
')" />
<input type="button" name="minus" value=" - " onclick="updateString('
- ')" />
<input type="button" name="times" value=" x " onclick="updateString('
* ')" />
<input type="button" name="div" value=" / " onclick="updateString(' /
')" />
<input type="button" name="mod" value=" mod " onclick="updateString('
% ')" /><br />
<input type="button" name="zero" value=" 0 " onclick="updateString(' 0
')" />
<input type="button" name="one" value=" 1 " onclick="updateString(' 1
')" />
<input type="button" name="two" value=" 2 " onclick="updateString(' 2
')" />
<input type="button" name="three" value=" 3 " onclick="updateString('
3 ')" />
<input type="button" name="four" value=" 4 " onclick="updateString(' 4
')" /><br />
<input type="button" name="five" value=" 5 " onclick="updateString(' 5
')" />
<input type="button" name="six" value=" 6 " onclick="updateString(' 6
')" />
<input type="button" name="seven" value=" 7 " onclick="updateString('
7 ')" />
<input type="button" name="eight" value=" 8 " onclick="updateString('
8 ')" />
<input type="button" name="nine" value=" 9 " onclick="updateString(' 9
')" />
<br />

<!-- // the onclick event for the calc button performs the calculation
using the eval() function with the input string variable -->
<!-- // the calculated value is then assigned as the value of the
input text box -->

<input type="button" name="point" value=" . " onclick="updateString('
. ')" />
<input type="button" name="clear" value=" clear "
onclick="document.Calculator.Input.value=''; inputString=''" />
<input type="button" name="calc" value=" = "
onclick="document.Calculator.Input.value=eval(inp utString);
inputString=''" />

</form>

</body>

</html>


Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Trvl Orm | last post by:
I am working with 2 frames, Left and Right and the main code is in the left frame, which has been attached. Can someone please help me with this code. I am new to JavaScript and can't figure it...
9
by: CW | last post by:
I wrote an HTML based chat application. The front end is built entirely on HTML + javascript. Essentially, I have a hidden frame that's refreshed frequently and any new messages are displayed in...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
1
by: Brent Ritchie | last post by:
Hello, I put up a small web application called SVG Creator, I'm developing it as a learning exercise. The url is: http://www3.sympatico.ca/brentritchie/. So far development is going well, but;...
2
by: rich | last post by:
I'd like to improve my webdesign knowledge and learn how to write Javascripts. I have built my own website. I have javascripts on my site that I havent written. I download them and edit them where...
4
by: DjShifta | last post by:
Ok this really shouldn't be that hard...but I can not figure out whats' wrong for the life of me. I am brand new to "coding" and have thus far just built basic sites with Dreamweaver, but am going...
60
by: marss | last post by:
Maybe anyone know good free online JavaScript knowledge test? This not exactly a system for testing online required - it may be simply list of questions with variants of answers (I have to prepare...
4
by: somenath | last post by:
Hi All, I have some questions about the process of learning C language. I see in this groups all expert most of the time quote from C standard. 1) My question is do all of you study the C...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.