如何在JavaScript中使用此函数中的“^”?

时间:2017-05-17 14:34:02

标签: javascript

  

编写一个名为“computeCompoundInterest”的函数。

     

给定本金,利率,复利频率和a   时间(以年为单位),“computeCompoundInterest”返回的数量   复利产生。

var output = computeCompoundInterest(1500, .043, 4, 6);
console.log(output); // --> 438.8368221341061

参考: https://en.wikipedia.org/wiki/Compound_interest#Calculation_of_compound_interest 这显示了用于计算生成的复合利息总数的公式。

问题是我试图使用这个公式并且无法正确使用

function computeCompoundInterest(p, i, compoundingFrequency, timeInYears) {

  p = p * (1 + (i/4)))^(compoundingFrequency*timeInYears)

}

我试图逐步完成每个计算,看起来就像我到达:

p = 1500 * (1 + 0.043/4)^ 4 x 6 //compoundingFrequency = 4 and timeInYears = 6

我做错了什么。当(1 + (i/4)))^(compoundingFrequency*timeInYears)

时,此website似乎会得到一个十进制数字

1 个答案:

答案 0 :(得分:4)

Math.pow(2,3) === 8运算符为XOR operator

对于指数,你应该使用函数Math.pow(如2**3 === 8)或** operator(如 <div class="row"> <div class="form-group col-sm-6"> <label for="limitClass">Limit Class</label> <select class="btn-group form-control" [(ngModel)]="model.limitClass" ngControl="limitClass" #limitClass="ngModel" (ngModelChange)="onLimitClassSelect($event)" name="limitClass"> <option *ngFor="let limitClass of limitClasses" [ngValue]="limitClass.id"> {{limitClass.name}} </option> </select> <div [hidden]="limitClass.valid || limitClass.pristine" class="alert alert-danger"> Limit Class is required </div> </div> <div class="col-sm-6"></div> </div> </form> </modal-content> <modal-footer> <button type="button" class="btn btn-default" data-dismiss="modal" (click)="close()">Cancel</button> <button type="reset" form="form1" class="btn btn-primary" (click)="initializeModal()">Clear</button> <button type="submit" form="form1" class="btn btn-primary">Save</button> </modal-footer>