[英]Understand Function Javascript
Please help me to understand this code.请帮助我理解这段代码。
function printEven(arr, pow) {
var res = 1;
for (var i = 0; i < pow; i++) {
res = res * arr;
}
return res;
}
console.log(printEven(4,3))
As far as I can see it is just a custom implementation of an exponential function.据我所知,它只是指数函数的自定义实现。
It effectively calculates arr^pow
, or in this case 4**3
or 4 * 4 * 4
.它有效地计算
arr^pow
,或者在这种情况下4**3
或4 * 4 * 4
。
At i = 0
, res gets multiplied with arr, 1 * 4 = 4
.在
i = 0
处, res 与 arr, 1 * 4 = 4
相乘。
At i = 2
it will have calculated 4 * 4 * 4
, returning 64 .在
i = 2
时,它将计算4 * 4 * 4
,返回64 。
It seems to simply apply a power, so printEven(2,5)
returns 2^5 or 32它似乎只是简单地应用一个幂,所以
printEven(2,5)
返回 2^5 或 32
it's effectly multipling the first input arr to 1 pow times (like 1*2*2*2*2*2
)它实际上将第一个输入 arr 乘以 1 pow 时间(如
1*2*2*2*2*2
)
so console.log(printEven(4,3))
is 64所以
console.log(printEven(4,3))
是 64
function printEven(arr, pow) { var res = 1; for (var i = 0; i < pow; i++){ res = res * arr; } return res; } console.log(printEven(4,3)) console.log(printEven(5,2))
This code is to find "number ^ times"此代码是查找“数字^次”
example 4 ^ 3 = 4 * 4 * 4 = 64示例 4 ^ 3 = 4 * 4 * 4 = 64
https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=RBAS_OEXPONENT#:~:text=Used%20to%20raise%20a%20number%20to%20the%20power%20of%20an%20exponent. https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=RBAS_OEXPONENT#:~:text=Used%20to%20raise%20a%20number%20to%20the%20power%20of %20an%20 指数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.