Skip to content
Advertisement

Translate JS into math (multiple variables defined)

I have this code I am trying to translate into maths

var Top=1+MonthlyInterestRate;

Top=Math.pow(Top,npr);'

Top=MonthlyInterestRate*Top;

var Bottom=1+MonthlyInterestRate;

Bottom=Math.pow(Bottom,npr)-1;

var MonthlyPayment=(PrincipalBalance*(Top/Bottom)).toFixed(2);

My basic problem is that the var ‘top’ is declared 3 times, so I don’t know how to reflect it mathematically.

var bottom is also declared twice, which value will be the final?

the first variable or the second?

Advertisement

Answer

JavaScript is imperative, instructions are executed from top to bottom. Here’s what happens, in order:

  1. Top equals 1 + MonthlyInterestRate,
  2. Top is set to Top to the power of npr
  3. Top is set to Top multiplied by MonthlyInterestRate
  4. Bottom equals 1 + MonthlyInterestRate,
  5. Bottom is set to Bottom to the power of npr minus 1
  6. then finally the result of Top divided by Bottom is multiplied by PrincipalBalance

A mathematical equation would be something along the lines of:

where a equals to PrincipalBalance, b equals to MonthlyInterestRate and c equals to npr

equation

Advertisement