简体   繁体   English

javascript代码在while循环后将无法运行

[英]javascript code won't run after while loop

I am trying to write a loop that inspects a string to see if a stored variable is contained within the string, and then replaces that variable with the stored value. 我正在尝试编写一个循环,该循环检查字符串以查看字符串中是否包含存储的变量,然后将该变量替换为存储的值。 I have that part working fine, however after my while loop the code stops and will not run. 我的那部分工作正常,但是在我的while循环之后,代码停止并且无法运行。 Here is the code: 这是代码:

 var output = document.getElementById('output'); String.prototype.replaceAll = function(search, replacement) { var target = this; return target.split(search).join(replacement); }; var sortedVarArray = [ //[varName, value, units, complex?] ["d", 2, null, 0], ["h", 3, null, 0], ["u", "d + h", null, 1] ]; var value = 'u + 1'; var complex = true; while (complex) { complex = false; for (var i = 0; i -1 < sortedVarArray.length; i++) { if (value.indexOf(sortedVarArray[i][0]) > -1 && sortedVarArray[i][3] == 1) complex = true; value = value.replaceAll(sortedVarArray[i][0], sortedVarArray[i][1]); if (complex) break; } output.innerHTML += "<br> hello"; } output.innerHTML += "<br> hello2"; var result = eval(value); output.innerHTML += "<br> result = " + result; 
 <div id="output"></div> 

so if, for example, I have an array sortedVarArray that looks like: 因此,例如,如果我有一个数组sortedVarArray看起来像:

sortedVarArray = [
    ["varName", "value", "units", "complex?"]
    ["d", 2, null, 0]
    ["h", 3, null, 0]
    ["u", "d + h", null, 1]
]

and an equation value equal to u + 1 方程value等于u + 1

which, as the loop progresses, should change to look like this: 随着循环的进行,它应该变为如下所示:

u + 1
d + h + 1
2 + h + 1
2 + 3 + 1

which will then evaluate to 6 at the end 然后最后将评估为6

My problem is that the code does not make it out of the for loop unless it breaks , and then it does not make it out of the while loop. 我的问题是,除非代码不breaks ,否则代码不会使其脱离for循环,然后就不会使其脱离while循环。 and my code never evaluates. 而且我的代码从不评估。

I have a bunch of output messages in my code (cleaned up for here) and I can see that the string value is being modified as desired as the code loops. 我的代码中有一堆输出消息(在这里进行了清理),我可以看到在代码循环时按需要修改了字符串value I am just not sure why it is not continuing after the loop ends. 我只是不确定为什么循环结束后它没有继续。

Oh and if I change the code so that complex is declared as false, the loop is skipped and "hello2" is printed to output . 哦,如果我更改代码以使complex声明为false,则会跳过循环,并output "hello2"output

Thanks in advance, 提前致谢,

Dan

EDIT: the ReplaceAll function is contained in the start of the script (outside of any function) and is as follows: 编辑:ReplaceAll函数包含在脚本的开头(任何函数之外),如下所示:

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.split(search).join(replacement);
};

in your for loop: 在您的for循环中:

i - 1 < sortedVarArray.length

will iterate your array for index 0 to length, where array[length] = undefined. 将迭代索引为0的数组的长度,其中array [length] = undefined。 try: 尝试:

i < sortedVarArray.length

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

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