Brief :
use parseInt or Math.floor to have y/2 as integer, unleness you will not reach 0 which is the stopper of recursion .
Details
if you want to transalte [C Algo]:
int power(int x, unsigned int y)
{
if( y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
To [JS Algo] , you will have :
function power(x,y){
if(y===0){return 1}
else if (y%2 ===0){
return power(x,parseInt(y/2))*power(x,parseInt(y/2))
}else{
return x*power(x,parseInt(y/2))*power(x,parseInt(y/2))
}
}
DEMO :
function power(x,y){
if(y===0){return 1}
else if (y%2 ===0){
return power(x,parseInt(y/2))*power(x,parseInt(y/2))
}else{
return x*power(x,parseInt(y/2))*power(x,parseInt(y/2))
}
}
console.log(power(3,2))
Answer from Abdennour TOUMI on Stack OverflowMDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › pow
Math.pow() - JavaScript | MDN
The Math.pow() static method returns the value of a base raised to a power. That is
W3Schools
w3schools.com › jsref › jsref_pow.asp
W3Schools.com
Math.pow() is an ECMAScript1 (JavaScript 1997) feature.
algorithm - JavaScript implementation of Math.pow - Stack Overflow
Here's a tail recursive ES2015 solution: const power = (base, exp, acc = 1) => exp === 0 ? acc : power(base, exp - 1, base * acc). ... Take a look at iterative (non recursive) approaches on integer math for some additional ideas: Power by squaring for negative exponents More on stackoverflow.com
JavaScript Exponents Explained - Math.pow() Examples
Math Pow Math.pow() returns the value of a number to the power of another number. Syntax Math.pow(base, exponent) , where base is the base number and exponent is the number by which to raise the base . pow() is a static method of Math , therefore it is always called as Math.pow() rather than ... More on forum.freecodecamp.org
Using a loop instead of math.pow()
What are the constraints on exponent? Can it be zero? Can it be negative? Can it be non-integer? More on reddit.com
What does Math.Pow(x,2) do under the hood, that is much slower than x*x?
Math.Pow needs to be able to raise any number to the power of any other number, not just 2. It's instrinsically a much slower computation than just multiplying one number by another. CPUs generally don't even have a single instruction for raising an arbitrary number to an arbitrary power - they have instructions for taking natural logarithms, and raising e to the power of some number, so they combine them to do: y = e^(n*ln(x)) More on reddit.com
Videos
01:58
JavaScript Basics - Math.pow() - YouTube
03:17
Math Pow JavaScript - YouTube
01:34
How to calculate power of a number in javascript Math.pow() and ...
- YouTube
JavaScript Math Pow | JavaScript Tutorial For Beginners - YouTube
06:44
JavaScript tips — The exponentiation operator (**) - YouTube
Math.js
mathjs.org › docs › reference › functions › pow.html
math.js
math.pow(2, 3) // returns number 8 const a = math.complex(2, 3) math.pow(a, 2) // returns Complex -5 + 12i const b = [[1, 2], [4, 3]] math.pow(b, 2) // returns Array [[9, 8], [16, 17]] const c = [[1, 2], [4, 3]] math.pow(c, -1) // returns Array [[-0.6, 0.4], [0.8, -0.2]]
Top answer 1 of 4
7
Brief :
use parseInt or Math.floor to have y/2 as integer, unleness you will not reach 0 which is the stopper of recursion .
Details
if you want to transalte [C Algo]:
int power(int x, unsigned int y)
{
if( y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
To [JS Algo] , you will have :
function power(x,y){
if(y===0){return 1}
else if (y%2 ===0){
return power(x,parseInt(y/2))*power(x,parseInt(y/2))
}else{
return x*power(x,parseInt(y/2))*power(x,parseInt(y/2))
}
}
DEMO :
function power(x,y){
if(y===0){return 1}
else if (y%2 ===0){
return power(x,parseInt(y/2))*power(x,parseInt(y/2))
}else{
return x*power(x,parseInt(y/2))*power(x,parseInt(y/2))
}
}
console.log(power(3,2))
2 of 4
2
Try this out
It will give you the same result of JavaScript build in method ( Math.pi(x, y)) but the only problem is you can't use Power as decimal number.
Math.my_pow = (x, y) => {
if (typeof x != "number" || typeof y != "number")
throw "(x) and (y) should only be number";
if (y == 0) return 1;
if (x == 0 && y > 0 ) return 0;
const base = x;
var value = base;
var pow = y;
if (y < 0) pow = y * -1;
for (var i = 1; i < pow; i++) {
value *= base;
}
if (y < 0) return 1 / value;
return value;
};
try {
console.log( Math.my_pow(0, -3) );
console.log( Math.pow(0, -2) );
console.log( Math.my_pow(-5, -3) );
console.log( Math.pow(-5, -3) );
console.log( Math.my_pow(8, -7) );
console.log( Math.pow(8, -7)) ;
} catch (err) {
console.log(err);
}
Programiz
programiz.com › javascript › library › math › pow
JavaScript Math.pow() (with Examples)
The pow() method computes the power of a number by raising the second argument to the power of the first argument.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Exponentiation
Exponentiation (**) - JavaScript | MDN
The exponentiation (**) operator returns the result of raising the first operand to the power of the second operand. It is equivalent to Math.pow(), except it also accepts BigInts as operands.
HCL Software
help.hcl-software.com › dom_designer › 9.0.1 › reference › r_wpdr_standard_math_pow_r.html
pow (JavaScript)
Gets a number raised to the exponential power of another number. Math (JavaScript) pow(x:double, y:double) : double · Special cases are as follows: The following yield NaN: exponent NaN · base NaN and exponent not 0 · base 1 and exponent Infinity · base less than 0 and exponent not integer ·
TutorialsPoint
tutorialspoint.com › home › javascript › javascript math.pow() function
JavaScript Math.pow() Function
September 1, 2008 - This method returns the value of x to the power of y (xy). In the following example, we are using the JavaScript Math.pow() method with postive and negative integer arguments −
Edgecompute
js-compute-reference-docs.edgecompute.app › math.pow()
Math.pow() | @fastly/js-compute
The Math.pow() method returns the value of a base raised to a power.
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Math › pow
Math.pow() - JavaScript
A number representing the given base taken to the power of the given exponent. The Math.pow() function returns the base to the exponent power, that is, baseexponent, the base and the exponent are in decimal numeral system.
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-pow-method
JavaScript Math pow() Method - GeeksforGeeks
July 15, 2024 - The Math.pow() method returns a number representing the given base raised to the power of the given exponent.
TechOnTheNet
techonthenet.com › js › math_pow.php
JavaScript: Math pow() function
In JavaScript, pow() is a function that is used to return m raised to the nth power. Because the pow() function is a static function of the Math object, it must be invoked through the placeholder object called Math.