JavaScript exponents
There is an exponentiation operator, which is part of the ES7 final specification. It is supposed to work in a similar manner with python and matlab:
a**b // will rise a to the power b
Now it is already implemented in Edge14, Chrome52, and also it is available with traceur or babel.
JS - Power sign / exponent to number
You could create a regular expression and perform one call of replace
, giving it a callback function:
let sup = { "⁰": "0", "¹": "1", "²": "2", "³": "3", "⁴": "4", "⁵": "5", "⁶": "6", "⁷": "7", "⁸": "8", "⁹": "9", "⁺": "+", "⁻": "-", "⁼": "=", "⁽": "(", "⁾": ")", "ⁿ": "n", "ⁱ": "i"};
let regex = RegExp(`[${Object.keys(sup).join("")}]`, "g");
// demolet s = "x⁽ⁱ⁻²⁾ * y³";let norm = s.replace(regex, c => sup[c]);console.log(s);console.log(norm);
Find simplest base and its exponent given product
The problem looks to reduce to finding the first number that divides the product (starting with the lowest possible first), then finding how many times it does.
A simple and more efficient way than yours would be to start at 2, then check 3, then increase by 2 for each following iteration, up until reaching the square root of the product. It's not the most efficient, but it's better and easy to implement.
const isDivisible = (a, b) => Number.isInteger(a / b);
const log = (n, base) => Math.log(n) / Math.log(base);
const findExponent = (product, base) => ({
base,
exponent: Math.round(log(product, base))
});
function findBaseAndExponent(product) {
if (isDivisible(product, 2)) return findExponent(product, 2);
for (let i = 3, limit = Math.floor(Math.sqrt(product)); i <= limit; i += 2) {
if (isDivisible(product, i)) return findExponent(product, i);
}
return { base: null, exponent: null };
}
console.log(findBaseAndExponent(343)); // Output: { base: 7, exponent: 3 }
console.log(findBaseAndExponent(64)); // Output: { base: 2, exponent: 6 }
console.log(findBaseAndExponent(41)); // Output: { base: N/A, exponent: N/A }
Difference between Exponent operator ^ and Math.pow()
^
isn't the exponentiation operator in JavaScript, **
is (and only recently). ^
is a bitwise XOR. More on JavaScript operators on MDN.
If you compare 100**49
to Math.pow(100,49)
, according to the specification, there should be no difference; from Math.pow
:
- Return the result of Applying the ** operator with base and exponent as specified in 12.6.4.
That may or may not be true of implementations at present, though, because again the exponentiation operator is quite new. For instance, as I write this, Chrome's V8 JavaScript engine returns very slightly different results for 100**49
and Math.pow(100,49)
: (Edit: As of 26/08/2020, they have the same result.)
console.log(100**49);
console.log(Math.pow(100,49));
JavaScript exponent in Number literal confusion
2e2
is interpreted as 2*(10^2)
and not (2*10)^2
. The former evaluates to 2 * 100
which equals 200. The latter evaluates to 20 ^ 2
which is why you are getting 400.
Javascript: While Loop to Solve Exponents Problem Getting Error When Power Equals 0
- You're accessing
result
before it is defined. so you should move them at the top of function Here you should return value directly instead of assignment
if (power === 0){
return result = 1
}
function exponentiate (base, power) { let count = 0 let result = 1; if (power === 0){ return 1 }
// while loop while (count < power){
result *= base count += 1 } return result }
console.log(exponentiate(3, 0))
How to find the value of exponent in Javascript
Try this. Refer to this link .
Math.log2(x) // x=64
The link
will also tell you about other related Math
functions.
Also remember that for finding log
of x
to the base y
, you can always use the formula ln(x)/ln(y)
and for ln
you already have an inbuilt function (Refer link above).
Related Topics
Setting Multiple Attributes for an Element at Once with JavaScript
How to Remove Spaces from a String Using JavaScript
Vuejs and Vue.Set(), Update Array
Firestore: Query by Item in Array of Document
JavaScript - Return String Between Square Brackets
Jquery Difference Between Change and Click Event of Checkbox
How to Intercept All Ajax Requests Made by Different Js Libraries
Javascript: Uncaught Typeerror: Cannot Call Method 'Addeventlistener' of Null
Pass Arguments with Page.Evaluate
How to Require a Controller in an Angularjs Directive
Download File from Bytes in JavaScript
How to Send an Ajax Request on a Different Port with Jquery
Garbage Collection with Node.Js
Why Use Object.Prototype.Hasownproperty.Call(Myobj, Prop) Instead of Myobj.Hasownproperty(Prop)
How to Upload a File Using Jquery.Ajax and Formdata
Sort Objects in an Array Alphabetically on One Property of the Array