- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Checking if a number is some power of the other JavaScript
We are required to write a JavaScript function that takes in two numbers, let’s say m and n. The function should check whether m is some power of n or not.
If it is, then we should return true, false otherwise.
For example −
m = 8, n = 2 should return true m = 100, n = 10 should return true m = .01, n = 10 should return true m = 21, n = 3 should return false
Example
const isPower = (m, n) => { let sign = m < 1; if (!m) { return false; }; while (m !== 1) { if (sign) { m *= n; } else { m /= n; } if (sign ? m > 1 : m < 1) { return false; } }; return true; } console.log(isPower(0.01, 10)); console.log(isPower(1000, 10)); console.log(isPower(1001, 10)); console.log(isPower(8, 2)); console.log(isPower(0.125, 2));
Output
This will produce the following output −
true true false true true
- Related Articles
- Checking if a number is a valid power of 4 in JavaScript
- Checking power of 2 using bitwise operations in JavaScript
- Checking if an array is sorted lexicographically in reference to some scrambled alphabet sequence in JavaScript
- Check if a number is a power of another number in C++
- Checking for a Doubleton Number in JavaScript
- Nearest power 2 of a number - JavaScript
- Check if given number is a power of d where d is a power of 2 in Python
- Checking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript
- Checking if a key exists in a JavaScript object
- Check if a number is power of 8 or not in C++
- How to check if a number is a power of 2 in C#?
- How to get the exponent power of a number in JavaScript?
- Checking digit sum of smallest number in the array in JavaScript
- Checking if two arrays can form a sequence - JavaScript
- Rounding off numbers to some nearest power in JavaScript

Advertisements