- 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 a valid power of 4 in JavaScript
Problem
We are required to write a JavaScript function that takes in a single integer, num, as the only argument. Our function should check whether this number is a valid power of 4 or not. If it is a power of 4, we should return true, false otherwise.
For example, if the input to the function is −
const num1 = 2356; const num2 = 16;
Then the output should be −
const output1 = false; const output2 = true;
Example
The code for this will be −
const num1 = 2356; const num2 = 16; const isPowerOfFour = (num = 1) => { let bool = false; for(let i = 0; i < 16; i++){ if( Math.pow(4,i) === num){ bool=true; return bool; }; }; return bool; }; console.log(isPowerOfFour(num1)); console.log(isPowerOfFour(num2));
Output
And the output in the console will be −
false true
- Related Articles
- Checking if a number is some power of the other JavaScript
- Check if a number is a power of another number in C++
- Check if a given string is a valid number in Python
- Check if a given string is a valid number in C++
- Checking for a Doubleton Number in JavaScript
- Checking power of 2 using bitwise operations in JavaScript
- Check if given number is a power of d where d is a power of 2 in Python
- Java Program to check if a string is a valid number
- Checking if a key exists in a JavaScript object
- Nearest power 2 of a number - JavaScript
- Find whether a given number is a power of 4 or not in C++
- How to check if a number is a power of 2 in C#?
- Check if a number is power of 8 or not in C++
- Checking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript
- Checking if a string can be made palindrome in JavaScript

Advertisements