简体   繁体   English

inputVal上的Math.pow()

[英]Math.pow() on inputVal

I don't know how to use the Math.pow() when I have 1 variable that gets my inputs which is inputVal . 当我有1个获取输入的变量inputVal时,我不知道如何使用Math.pow() This is how I did it. 这就是我做的。

else if(pow == true){
    if(equation.indexOf('^') == inputVal){
        inputVal = inputVal.replace('^', '');
        input.innerHTML = Math.pow(inputVal);
    }else{
        input.innerHTML = 'Syntax Error';
        pow = false;
    }
}

I tried to do this on my own, and it doesn't work. 我试图独自执行此操作,但它不起作用。

I've also tried this 我也试过了

else if(pow == true){
    input.innerHTML = Math.pow(inputVal);
}else{
    pow = false;
}

Presumably, your input value is of the form base^exponent . 大概您的输入值的格式为base^exponent So, split the input on ^ to get an array of arguments and then call Math. pow () 因此,将输入拆分为^以获取参数数组,然后调用Math. pow () Math. pow () using the spread operator , ... ; Math. pow ()使用价差运算符... ; eg: 例如:

 console.log("result: %o", Math.pow(..."2^3".split("^"))); 

Math.pow() needs 2 parameters. Math.pow()需要2个参数。 If your inputVal is aa^b format what you will need to do is 如果您的inputVal是aa ^ b格式,则需要做的是

var piv = inputVal.indexOf("^");
var a = parseInt(inputVal.substring(0, piv));
var b = parseInt(inputVal.substring(piv+1));
input.innerHTML = Math.pow(a, b);

Please make sure your inputVal is a string. 请确保您的inputVal是字符串。 otherwise the string function wont work. 否则字符串功能将无法正常工作。 the comments from conan is correct. 柯南的评论是正确的。 you can try parseFloat or even Number() function according to your needs. 您可以根据需要尝试parseFloat甚至Number()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM