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))
CodePudding user response:
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
.
At i = 0
, res gets multiplied with arr, 1 * 4 = 4
.
At i = 2
it will have calculated 4 * 4 * 4
, returning 64.
CodePudding user response:
It seems to simply apply a power, so printEven(2,5)
returns 2^5 or 32
it's effectly multipling the first input arr to 1 pow times (like 1*2*2*2*2*2
)
so console.log(printEven(4,3))
is 64
CodePudding user response:
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