Home > Software engineering >  Converting Excel function to TypeScript function
Converting Excel function to TypeScript function

Time:04-15

I'm trying to convert the following Excel's function (factor =1/(1 disc_rate)^(A2-base_yr) to TypeScript function. TypeScript is new to me, can look at this code and let me know what I'm doing wrong? I'm getting 0 value when running the code using the TypeScript Playground environment. Also, not sure if the Math.pow statement is correct. I'm basically trying to replicate the Excel's factor formula in TypeScript. Thanks

var base = 10
var rate = 7
var risk = 0.5
function npv(base: number, rate: number, risk: number){
    var factor = Math.pow((1/(1 rate)), base)
    return factor * risk
}
console.log(npv(base, rate, risk))

CodePudding user response:

You mentioned you're not sure if the Math.pow statement is correct.

For your specific use case it should be:

var factor = 1 / Math.pow(((1 rate)), base)
  • Related