Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Javascript help!!!! willing to pay via paypal!?
I need to build a basic calculator that does Addition.
Users must input the start and ending boundaries. Limit the inputs to positive whole number less than 301. The first number must be less that or equal to the end number.
The web calculator computes the sum of the numbers between the boundaries.
For example: The user inputs 1 and 10. The calculator sums the numbers between 1 and 10 (1+2+3+4+5+6+7+8+9+10) and outputs the resulting 5
how would i go about doing this?? I know how to build the HTML form portion, but i'm completely lost on the javascript...anyone who can help me, i'd be willing to pay generously via paypal...thank you
<html>
<title> Liz's Addition Calculator! </title>
<head>
<script language="javascript">
function calculate()
{
var a = getElementbyId.form.A.value;
var b = getElementbyId.form.B.value;
sum = (a+b) * (b-a+1) / 2;
}
{
alert("The answer is" +sum);
}
</script>
</head>
<body>
<FORM>
<Input type="text" id="A" value="">
<input type="text" id="B" value="">
<input type="button" name="submit" value="Calculate" onClick="calculate();">
</form>
</body>
</html>
what am i doing wrong??? it wont work!
4 Answers
- 1 decade agoFavorite Answer
A very interesting task!
What are you doing wrong? Not much, you're almost there ;-)
The above formula(s) are correct.
Your code has just a few bugs, please compare to the below:
<html>
<head>
<title> Liz's Addition Calculator!</title>
<script type="text/javascript">
function calculate() {
var a = +(document.getElementById("A").value);
var b = +(document.getElementById("B").value);
sum = (a+b) * (b-a+1) / 2;
alert(sum);
}
</script>
</head>
<body>
<Input type="text" id="A" value="">
<input type="text" id="B" value="">
<input type="button" value="Calculate" onClick="calculate()">
</body>
</html>
(if you're interested in the derivation of the above formula,
please eMail me: a@4nf.org)
I've hosted this for you:
Source(s): http://4nf.org/html/ya It works. - 1 decade ago
I'd also suggest you set input type to "number".
The code:
<html>
<head>
<title> Liz's Addition Calculator!</title>
<script type="text/javascript">
function showSum() {
var i = +(document.yourFormName.first.value);
var j = +(document.yourFormName.last.value);
var sum =[(j*j+j)/2]-[(i*i+i)/2]+i;
document.yourFormName.result.value=sum
}
</script>
</head>
<body>
<form name="yourFormName">
<input type=number name="first" size=8 value=""><br>
<input type=number name="last" size=8 value=""><br>
<input type=number name="result" size=8 value=""><br>
<input type="button" value="Calculate" onClick="showSum()">
</form>
</body>
</html>
Source(s): Check it out here http://www.findstorenearu.com/addnumbers.html - just "JR"Lv 71 decade ago
Although Cathy's formula will give you the correct answer, it is way over the top!
a = first integer number
b = last integer number
sum = (a+b) * (b-a+1) / 2
(Have you forgotten your maths on "series" ??? )
Source(s): pro web2 application developer at www.web2coders.com