Three or more roots in js

<script>
a = 3;
a = a^a // a = 27
</script>

there is something like this:

+3
source share
3 answers

Math.pow :

var a = 3;
a = Math.pow(a, a);
+13
source
// You can pass '3^3' to a method if you like-

Math.toPow= function(s){
    s= s.split('^');
    return Math.pow(+s[0], +s[1]);
}

// or to validate input-

Math.toPow= function(s){
    s= s.split('^');
    try{
        return Math.pow(+s[0], +s[1]);
    }
    catch(er){
        return NaN;
    }
}

Math.toPow('3^3')

/*  returned value: (Number)
27
*/

// I prefer to use Math.pow(3,3)
Math.pow(3,3)

/*  returned value: (Number)
27
*/
0
source

Source: https://habr.com/ru/post/1763220/


All Articles