Implementation in js to find if N can be written as X ^Y , N<100 From past year ,i have been mainly working on Js, and so have started implementing Algo Questions in js .can it be implemented in a better way.

/**
* Implementaion to find if a number can be expressed as x^y
* x is assumed to be &gt;=0 
*/

var canBeExpressedAsXraisedToY = function (n) {
if (n == 0 &amp;&amp; n == 1) {
    console.log("Supplied 0 or 1");
    return true;
}
else if (n &gt; 1) {
    var x = 2, prod = 1,y = 1;
    while (prod &lt; n) {

        while (prod &lt; n ) {

            prod = Math.pow(x,y);
            console.log('prod:',prod,'y:', y ,'x:',x)
            if (prod == n) {
                console.log('it can be expressed as x:', x, 'in terms of:', y);
                return true;
                break;
            }
          y++;
        }
        x++;
    }

    console.log("the number cannot be expressed as x ^ y ");
    return false;
}
else { // to handle negative cases .
    console.log('the number given is not supported ');
    return false;
}
}

console.log(canBeExpressedAsXraisedToY(4));
console.log(canBeExpressedAsXraisedToY(67));
console.log(canBeExpressedAsXraisedToY(64));
The algorithm is wrong. canBeExpressedAsXraisedToY(81) returns false.

Of course any n can have a trivial solution x=n and y=1.  I assume you want to require y>1. But you didn't state that anywhere, and in fact initialize y=1 in your code.

Initializing prod=1 is sloppy programming. You should structure the loops to compute prod before testing it.

That is pretty much a brute-force exhaustive search. For large n, a much better strategy would be to analyze the prime factors of n instead.