代码目标:
此JavaScript代码将接受用户在输入框中给出的字符串输入,它将通过将字符串转换为多个数组,连接其成员,然后使用条件语句来检查字符串中的某些字符。 然后,根据结果,它将调用函数并将函数的结果存储在变量/变量中。
问题:
这是我的JavaScript代码:
results = []
numbers = [1, 3, 5]
for number in numbers:
for i in range(len(X)):
if number in X[i]:
results.append(i)
function start() {
var one_one = document.getElementById("oneOne").value; // HTML : <tr><td><input type = "text" name = "one_one" id = "oneOne" value = " " /></td></tr>
function logAnyBase(x, base) {
var rslt = (Math.log(x)) / (Math.log(base));
return rslt;
}
var result;
var array = one_one.split("^");
var arrayTwo = array[1].split("= ");
var numberRaised = Number(array[0]);
var equationEquals = Number(arrayTwo[1]);
var arr = one_one.split('');
var indexOfVariableX = arr.indexOf("x");
var indexOfToThePowerOfSign = arr.indexOf("^");
var variableX = arr[indexOfVariableX];
var toThePowerOfSign = arr[indexOfToThePowerOfSign];
var checkLogCase = toThePowerOfSign + variableX;
if (checkLogCase == "^x") {
result = logAnyBase(equationEquals, numberRaised);
} else {
var equation = algebra.parse(one_one); // a function from algebra.js, which converts string into a mathematical equation. Example: x + 2 = 3
result = equation.solveFor("x"); // this function finds the solution for x in this equation. For example: if the equation is x + 2 = 3, the solution is 1.
}
var final = document.getElementById("final"); // HTML: <h3 id = "final"></h3>
final.innerHTML = "x = " + String(result);
}
但是,当我运行代码时,只有if块工作,忽略else块。这意味着,如果我将2 ^ x = 4作为输入,则结果是正确的,即“x = 2”。但是当我给出其中没有“^ x”的东西时,例如“x + 2 = 3”,那么它就不会返回任何东西。
-
<script src = "https://algebrajs.000webhostapp.com/algebra-0.2.6.js"></script>
<div>
<tr>
<td>
<input type = "text" name = "one_one" id = "oneOne" value = " "/>
</td>
</tr>
<br />
<br />
<button onclick = "start()">Solve</button>
<h3 id = "final" style="color:black; overflow:scroll;"></h3>
</div>
-
var one_one = document.getElementById("oneOne").value;
var equation = algebra.parse(one_one);
var result = equation.solveFor("x");
var final = document.getElementById("final");
final.innerHTML = "x = " + String(result);
我想,如果我给“2 ^ x = 4”,代码将返回“x = 2”,如果我将“x + 2 = 3”作为输入,它将返回“x = 1" 。
如果我在问题中有任何缺陷,请将它们指出。
答案 0 :(得分:0)
我意识到我的问题出现在第11行,我写了var arrayTwo = array[1].split("= ");
。它应该是var arrayTwo = one_one.split("= ");
。
可能是.split()函数不支持直接引用数组元素。
无论如何,谢谢你的帮助。 : - )