aka NatWest
10-20-2009, 05:57 PM
Ok, I have spent ages attempting to make a basic addition calculator in Javascript for my webpage, however I am new to all this and can't grasp it, so I just want to complete my course and maybe look at my other options :/
What I need is a JavaScript script:
Calculator
Enter a number to be calculated:
(A box where the user can enter a number) + (another box where a user can enter a number) = (a box with the answer shown)
(a 'add' button underneath the first box)
However my course says that I must edit this script:
<html>
<head>
<title>The Adding Calculator</title>
<script language="Javascript">
<!--
function Calculate()
{
var first = document.form1.first.value;
var second = document.form1.second.value;
if(document.form1.first.value == 0)
{
alert("Please ensure that you have entered a number into each of the boxes");
return false;
}
if (document.form1.second.value == 0)
{
alert("Please ensure that you have entered a number into each of the boxes");
return false;
}
addition = parseInt(first) + parseInt(second);
total = first + " " + " + " + second + " " + "=" + " " + addition;
alert(total);
}
//-->
</script>
</head>
<body>
<h1> The Adding Calculator </h1>
<form name="form1">
Enter some numbers in these boxes:<br>
<br>
<input type="text" name="first" size="20">+<input type="text" name="second" size="20"> <input type="button" value="Calculate" onClick="Calculate()">
</form>
</body>
</html>
And incorporate the eval() function - but I have no idea how to do this or what this is :/
I am new here so sorry if this is in the wrong place or if I have messed anything else up.
I hope this makes sense. Thanks for any help/advice!
What I need is a JavaScript script:
Calculator
Enter a number to be calculated:
(A box where the user can enter a number) + (another box where a user can enter a number) = (a box with the answer shown)
(a 'add' button underneath the first box)
However my course says that I must edit this script:
<html>
<head>
<title>The Adding Calculator</title>
<script language="Javascript">
<!--
function Calculate()
{
var first = document.form1.first.value;
var second = document.form1.second.value;
if(document.form1.first.value == 0)
{
alert("Please ensure that you have entered a number into each of the boxes");
return false;
}
if (document.form1.second.value == 0)
{
alert("Please ensure that you have entered a number into each of the boxes");
return false;
}
addition = parseInt(first) + parseInt(second);
total = first + " " + " + " + second + " " + "=" + " " + addition;
alert(total);
}
//-->
</script>
</head>
<body>
<h1> The Adding Calculator </h1>
<form name="form1">
Enter some numbers in these boxes:<br>
<br>
<input type="text" name="first" size="20">+<input type="text" name="second" size="20"> <input type="button" value="Calculate" onClick="Calculate()">
</form>
</body>
</html>
And incorporate the eval() function - but I have no idea how to do this or what this is :/
I am new here so sorry if this is in the wrong place or if I have messed anything else up.
I hope this makes sense. Thanks for any help/advice!