【发布时间】:2020-12-09 16:02:45
【问题描述】:
我的问题与上面描述的完全一样。我在下面有一些代码定义了数组中设置的变量。已经存在的代码只是删除了文本框中定义变量的区域,以便您得到答案。您可以只定义变量,也可以在定义变量后编写 [simp] 以获得简化的答案,而不仅仅是交换变量。例如,我希望你能够在文本框中写 1+a[a=1],并在下面的文本中得到 1+1。
<!DOCTYPE html>
<html>
<head>
<title>PlayBox</title>
<style>
textarea {
resize: none;
width: 100%;
height: 100px;
}
body{
font-family: 'Courier New', Courier, monospace;
background-color: white;
}
</style>
</head>
<body>
<textarea id="input"></textarea>
<button onclick="run()">Run</button>
<p id="output"></p>
<script>
function run() {
var input = document.getElementById('input').value
var simpPositive = input.search('[simp]')
var doctorPositive = input.search('[doctor]')
var defineVarPositive = input.search(/\[\D\=\d/)
if ((simpPositive > -1) & (defineVarPositive < 0)) {
var newInput1 = input.replace('[simp]', '').replace(/(?<=[\d\)])\((?=\d)/g, "*(")
var simp = eval(newInput1)
document.getElementById('output').innerHTML = simp
}
if (doctorPositive > -1) {}
if (defineVarPositive > -1) {
var findMultVar = input.split('[')
var elem = findMultVar[1];
var fixFindMultVar = elem.substring(0, elem.length -1)
var ffmvarr = fixFindMultVar.split(';')//array here
var replaceletter = input.split('[')[0]//need more code here to change the variables using the array
console.log(replaceletter)
if (simpPositive > -1) {
var deleteSimp = replaceletter.replace('[simp]', '')
var newInput2 = deleteSimp.replace(/(?<=[\d\)])\((?=\d)/g, "*(")
var simp = eval(newInput2)
document.getElementById('output').innerHTML = simp
} else {
document.getElementById('output').innerHTML = replaceletter
}
}
if ((simpPositive < 0) & (defineVarPositive < 0) & (doctorPositive < 0)) {
document.getElementById('output').innerHTML = 'error: no modifiers found';
}
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript