while循环在JavaScript中永远运行

时间:2016-05-03 19:56:47

标签: javascript loops while-loop

以下是我的代码,其中我尝试将每个数组项的大小减少2,一旦数组的特定值变为0-1,我将用{{替换它1}}。

示例 -

输入 - undefined

输出 -

[5,4,4,2,2,8]

代码(不知怎的,它作为一个无限循环) -

Array            Non Blank Values    
5 4 4 2 2 8          6        
3 2 2 _ _ 6          4        
1 _ _ _ _ 4          2        
_ _ _ _ _ 3          1        
_ _ _ _ _ _        DONE   

让我知道我在这里做错了什么。

1 个答案:

答案 0 :(得分:3)

问题是,您在<!DOCTYPE html> <!-- WARNING : modifying login.jsp may affect the login layout for Mobile, Embedded and Desktop Version. --> <html> <head> <!-- Import header for script/style ... --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Cache-Control" content="no-cache"/> <meta http-equiv="Expires" content="-1" /> <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> <link type="image/x-icon" rel="shortcut icon" href="resources/images/favicon.ico"> <link type="text/css" rel="stylesheet" href="resources/style/login.css"> <style type="text/css"> body{ background: url(resources/images/gradient_body_login.png) repeat-x; background-color: #d3d3d3; } #container{ background: url(resources/images/background_body.png) no-repeat top; width:1090px; min-height:770px; height:auto; } #container{ margin:auto; margin-top:0; } #login input[type="submit"],#login input[type="button"]{ margin:10px 5px 10px 0; } #mask{ opacity: 0.5; filter: alpha(opacity = 50); } </style> <script type="text/javascript" src="resources/script/live.js"></script> <script type="text/javascript"> <!-- Against XFS attack --> if(top != self) {top.location=self.location;} <!-- Against XFS attack --> function Start(){ DisplayContent(); GiveFocus("j_username"); <!-- Check login failed --> var vars = getUrlParameters(); var loginLabel = document.getElementById("failureIndication"); var authFailed = vars["authfailed"]; if (authFailed === "true") loginLabel.innerHTML = "Login Failed"; <!-- Check login failed --> } function DisplayContent() { var mainFrame = document.getElementById("hasJavascript"); mainFrame.style.display = 'block'; } function GiveFocus(id){ document.getElementById(id).focus(); } function setSubmitUrl(form){ var hash = getUrlHash(); if((hash != null) && (hash != "")) { form.action = "j_spring_security_check#" + hash; }else { form.action = "j_spring_security_check"; } return true; } </script> <!-- Sample of custom logo --> <style type="text/css"> h2 { background: url("resources/large.png") no-repeat 20px 0 transparent; line-height: 20px; padding-left: 170px; background-size: 75px 33px !important; background-position: 35px 0px !important; } body { font-family:Verdana;font-size:12px;color:#444;margin:0;padding:0;width:100%;height:100%; background-color: #5BBF19 !important; background-attachment: fixed !important; background-repeat: no-repeat !important} } #mask { display: none !important; } #login{ border:4px solid #008800 !important } </style> <title> Portal Login </title> </head> <body OnLoad="Start();"> <noscript> <div class="noJavascriptBox"> Your web browser must have JavaScript enabled in order for this application to display correctly. </div> </noscript> <div id="hasJavascript" class="hidden contentContainer"> <div id="container"> <div id="mask"></div> <div id="login"> <h2>Portal Live Login</h2> <form id="login_form" method="POST" onSubmit="return setSubmitUrl(this);"> <label for="j_username">Username:</label> <input type="text" id="j_username" name="j_username" autocapitalize="off" autocorrect="off"/> <label for="j_password">Password:</label> <input type="password" id="j_password" name="j_password" autocapitalize="off" autocorrect="off"/> <label id="failureIndication">&nbsp;</label> <input type="submit" value="OK"/> </form> </div> </div> </div> </body> 上调用了makeItemUndefine,但这并没有改变,因为您将结果分配给名为arr的其他变量。所以你实际上并没有随着时间的推移减少数组中的数字。

正确的解决方案是:

result

代码的另一个问题是,您尝试从未定义的数字中减去这些数字,并将其转换为while(itemNum != 0) { num ++; arr = makeItemUndefine(arr); itemNum = countItemNotUndefined(arr); } 。有两种解决方案,要么检查NaN内的NaN,要么检查地图内的countItemNotUndefined