- 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
Validating a power JavaScript
We are required to write a JavaScript function that in a number, say n, as the only input. The function should then validate if the input number is a power of 3 or not.
If it is a power of 3, we should return true, false otherwise.
For example −
isPowerOf3(243) = true isPowerOf3(343) = false isPowerOf3(81) = true
Example
const num = 243; var isPowerOf3 = (num = 3) => { let divisor = num === 1 ? 1 : 3; while(divisor < num){ divisor *= 3; }; return divisor === num; }; console.log(isPowerOf3(num)); console.log(isPowerOf3(343)); console.log(isPowerOf3(81));
Output
And the output in the console will be −
true false true
- Related Articles
- Validating a password using JavaScript
- Validating a boggle word using JavaScript
- Validating brackets in a string in JavaScript
- Validating email and password - JavaScript
- Validating a file size in JavaScript while uploading
- Validating push pop sequence in JavaScript
- Validating a square in a 2-D plane in JavaScript
- Validating alternating vowels and consonants in JavaScript
- Validating a string with numbers present in it in JavaScript
- Validating a Form with Parsley.js
- Validating string with reference to array of words using JavaScript
- Finding power set for a set in JavaScript Power Set
- Nearest power 2 of a number - JavaScript
- Way of validating RadioBoxes with jQuery?
- Check for Power of two in JavaScript

Advertisements