views:

81

answers:

6

How do I express this in javascript?

9H squared plus 3H all over 2 times L

I'm working on something like:

function calculator (height, len) {
var H = height, L = len;
total = (((9 * H)*(9 * H)) + 3*H)/2)*L;
return total;
}

calculator(15, 7);

I don't care if it's terse or not, but I'm not sure the best way to handle math in javascript.

thank you.

+1  A: 

Looks almost fine to me. What's the problem you're having?

The only thing I see wrong is a missing var before total, thus making it global. Change your code to:

function calculator (height, len) {
    var H = height,
        L = len, // <-- subtle change: replace ; with ,
        total = (((9 * H)*(9 * H)) + 3*H)/2)*L;
    return total;
}

Of course, you could also factor out the 9:

total = ((81*H*H + 3*H)/2)*L;

And if you want to get even fancier, then factor out the common 3*H as well:

total = (3*H*(27*H + 1)/2)*L;

But I'm not sure what else you're looking for here.

Matt Ball
9H^2 means 9*H*H, not 9*H*9*H
Andrew Cooper
@Andrew: that **totally** depends on the parentheses. What makes you think the (less precise) English-language expression of the formula is the one that the OP meant? Saying "9H squared" is ambiguous. It could mean either `(9H)^2` or `9(H^2)`, but I went with the expression of the problem that's more precise - the _code_.
Matt Ball
@Matt Ball: I agree with what you're saying about the ambiguity. I went with the other interpretation because it looks like a basic polynomial expression and I assumed, given the question, that the OP had made a mistake in getting the formula into code.
Andrew Cooper
+5  A: 

Squaring a number x can be represented as Math.pow(x, 2). Also, "all over 2 times L" would mean / (2 * L) at the end rather than the way you have it, if you do really mean:

  whatever
-----------
    2L

You are also missing the var keyword before total, which would declare it as a local variable.

idealmachine
+4  A: 

Horner's method is a nice way of expressing polynomials:

function calc (height, length) {
     return ((9*height + 3)*height)/(2*length);
}

http://en.wikipedia.org/wiki/Horner_scheme

xscott
+1  A: 

In "9H squared" it's only the H that is squared, so

function calculator (height, len) {
    var H = height, L = len;
    var total = (9*H*H + 3*H)/(2*L);
    return total;
}
Andrew Cooper
A: 

You might go about it like so...

function calculator (height, len) {
  var h = height, l = len;
  var total = ( 9 * Math.pow(h, 2) + 3*h ) / (2*l);
  return total;
}

Don't forget making a variable with out var prepended will make it global :)

David
+1  A: 

+1 to Andrew Cooper

(9*H)*(9*H) = 81*H^2, which i dont believe you intend

9*H*H = 9H^2 is how you intend that term

(9*H*H + 3*H) / (2*L)
or factor
(3*H)(3*H+1)/(2*L)

Which is equal to the sum of 1 + 2 + .. + 3H all divided by L (if H is an intger)
This last part probably doesn't do anything for you, but I like the identity =P

jon_darkstar
Yes, I think you are correct. As you can tell, I really struggle with math.
orolo