0

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.

1
  • 2
    You don’t need parseFloat. The result is correct. I don’t see how this would equal 102297. Commented Mar 13, 2017 at 2:10

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.