Galactic Ranger
Galactic Ranger

Reputation: 882

Having trouble converting a calculation in JavaScript

I am having trouble converting a calculation over into JavaScript. Any help would be greatly appreciated. Below please find an example of what I currently have.

Im having difficulty converting this into javascript:

(a * (1 + b) ^ c) + d * (((1 + b) ^ (c) - 1) / b)

Variables

var a = 1250;
var b = 0.03;
var c = 25;
var d = 3234;
var total = 0;

Method

var getTotal = function() {
    var exponentBase = 1 + parseFloat(b);
    total = a * (Math.pow(exponentBase, c)) + d * 
    ((Math.pow(exponentBase, c) - 1) / b)
};

My getTotal comes up to 120526.48 But from what I am being told it should be 102297

Again any help would be greatly appreciated.

Upvotes: 0

Views: 40

Answers (1)

Robin Mackenzie
Robin Mackenzie

Reputation: 19319

I'd rewrite the formula as:

(a * Math.pow((1 + b), c) + d * ((Math.pow((1 + b), c)) - 1) / b)

var aCalculation = function(a, b, c, d) {
  var total = 0;
  total = (a * Math.pow((1 + b), c) + d * ((Math.pow((1 + b), c)) - 1) / b);
  return total;
};

console.log(aCalculation(1250, 0.03, 25, 3234));

Excel agrees:

enter image description here

Upvotes: 2

Related Questions