Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Simple Math

1. ## Simple Math

Hi,

I am trying to create a javascript that says:

add a and b together and then divide it by 2.
(a + b) / 2

How would you do this?

• ## solution using functions

Code:
```function add(num1, num2) {
sum = num1 + num2;
return sum
}

function halve(num) {
half = num/2;
return half;
}

function sumAndHalve(numA, numB) {
numC = add(numA, numB);
numD = halve(numC);
return numD
}```
then, anywhere, you'd call
Code:
`sumAndHalve(3,1)`
I know there are easier, more terse ways to code this, but I like to keep each operation separate. Is that so wrong?

• ## D'oh!

I posted a new thread instead of posting a reply. D'oh! Check it.

<edit by="brothercake">
I merged your answer (above) into this thread
</edit>

• Or make some methods! I love methods!

Code:
```Number.prototype.add = function()
{
var arg, i = 0, sum = this;
while( arg = arguments[i++] )
{
sum += parseInt( arg, 10 );
}
return sum;
}

Number.prototype.divide = function( denom )
{
return this / denom;
}

var a = 4;
var b = 2;

alert( a.add( b ).divide( 2 ) );```

• thanks, brothercake.

beetle, great solution. For some reason, I don't seem to consider extending the prototype until long after the original need is fulfilled by functions. Dur!

• ok thanks. would i be able to use variables already defined as the addends?

• Wouldn't this be cooler to have on an Array?
Code:
```Array.prototype.sum=function(){
var
l=this.length,
r=0;
while(--l)
r+=this.[l];
return r;
}

Array.prototype.avg=function(){
var
k,
l=k=this.length,
r=0;
while(--l)
r+=this.[l];
return (r/k);
}```
Then you can use [a,b].avg() for doing the job.

• Cooler? Yes? More practical? Your mileage may vary

Choopernickel - extending the prototype is a large part of an upcoming article I'm writing for Sitepoint.com on javascript namespaces/scope It should be an eye-opener for many (I hope)

• Excellent, beetle. I've gotten familiar with extending it through some string operations I've come up with, then with an Array.search() method that I think was pretty cool.

I'm SO freakin' happy to finallly have found a javascript forum where it's like, all serious javascripting and everything! like you don't even know.

• Ok here's what I'm trying to do:

Code:
```Number.prototype.add = function() {
var arg, i = 0, sum = this;
while( arg = arguments[i++] ) {
sum += parseInt( arg, 10 );
} return sum;
}

Number.prototype.divide = function( denom ) {
return this / denom;
}

function calculate() {
var a = document.triangle.a.value
var b = document.triangle.b.value
var c = document.triangle.c.value
var b2 = b*b
var c2 = c*c

if (a=" ") {
document.triangle.a.value = b2.add(c2).divide( 2 )
}
else {
javascript:calculate2()
}
}```

For this one I want c - b, not c + b. I've spent ages on this, and can't firure it out.

• LOL Never Mind stupid question. I think I got it.

• Please tell me what I am doing wrong here!

Code:
```function divide(num) {
half = num/2;
return half;
}

function calculate() {
var a = document.triangle.a.value;
var b = document.triangle.b.value;
var c = document.triangle.c.value;
var b2 = b*b;
var c2 = c*c;
var a2 = a*a;
var c2b2 = c2-b2;

if (a=" ") {
document.triangle.a.value = divide(c2b2);
}
else {
alert("Hello");
}
}```
By the way, yes I am doing the pythagorean theorem. A few of the variables are not necessary now, but I need them later.

• All values retrieved from form elements are String-type variables. You need to convert them to Number-type with parseInt

var f = document.triangle;
var a = parseInt( f.a.value, 10 );
var b = parseInt( f.b.value, 10 );
var c = parseInt( f.c.value, 10 );

Which is why I suspect you also had problem using methods prototyped onto the Number object

• Ok I see what your doing, but what is the need for the 10?

• Also, I'm still getting an answer of 4.5, when I want three, with this code:

Code:
```function divide(num) {
half = num/2;
return half;
}

function calculate() {
var triangle = document.triangle;
var a = parseInt( triangle.a.value, 10 );
var b = parseInt( triangle.b.value, 10);
var c = parseInt( triangle.c.value, 10);
var a2 = a*a;
var b2 = b*b;
var c2 = c*c;
var c2b2 = c2-b2;

if (a=" ") {
document.triangle.a.value = divide(c2b2);
}
else {
alert("Hello");
}
}```

•
Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•