js中有三个或更多root

时间:2010-09-04 12:47:07

标签: javascript

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

有类似的东西:

3 个答案:

答案 0 :(得分:13)

Math.pow

var a = 3;
a = Math.pow(a, a);

答案 1 :(得分:0)

// 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
*/

答案 2 :(得分:-1)