'How can I make an if statement that asks, if the number equals to 8 to the power of n (8^n)? [closed]
I want to do an if statement that looks something like this:
(n is a natural number)
if (number == 8^n) {
// Code...
}
Solution 1:[1]
Use Math.pow(x, y);
Example:
function check(number,n){
let r = Math.pow(8, n);
if (number==r)
//Do something
}
Solution 2:[2]
var n=parseInt(prompt("Enter N:"));
var _number =parseInt(prompt("Enter Number:"));
if(!isNaN(n) && !isNaN(_number))
{
if(_number == Math.pow(8, n))
console.log("Equal");
else
console.log("Not Equal");
}else
console.log("Invalid N / Number");
Or
var n=parseInt(prompt("Enter N:"));
var _number =parseInt(prompt("Enter Number:"));
if(isNaN(n))
console.log("Invalid N");
else if(isNaN(_number))
console.log("Invalid N");
else
{
if(_number == Math.pow(8, n))
console.log("Equal");
else
console.log("Not Equal");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Elidor |
| Solution 2 | Peter Reda |
