简体   繁体   English

If / Else语句嵌套在While循环中

[英]If/Else statement nested in While loop

(I am very new to JavaScript and am writing this to try and get used to using it a bit more - which is why I'm using an object to store usernames and passwords. I'm learning this as part of an apprenticeship but this is during my own time to further my knowledge on JS) I have tried writing a small piece of code that should ask the user to enter a username, which will be cross-checked against the property names in the object " userNames ". (我对JavaScript非常陌生,正在编写此书来尝试并习惯于使用它-这就是为什么我使用一个对象来存储用户名和密码的原因。我在学徒时期学习了此方法,但这是为什么是在我自己的时间里,以进一步了解JS)我尝试编写一小段代码,要求用户输入用户名,该用户名将与对象“ userNames ”中的属性名称进行交叉检查。 The user is then asked to enter a password for this user which is then checked against the password set as the value of the corresponding property with the right username. 然后要求用户输入该用户的密码,然后将其与设置为具有正确用户名的相应属性的值的密码进行检查。 I feel as though part of the problem is to do with me trying to use the variable " enteredPassword " (set to the user's input for the password as a method) of " userNames ". 我觉得好像是问题的一部分是我尝试使用“ 用户名 ”的变量“enteredPassword”(设置为用户输入的密码的方法)来完成。 If the username or password are incorrect after 3 attempts then I'd like the window to close. 如果3次尝试后用户名或密码不正确,那么我想关闭窗口。 my text editor is saying that the If statement is missing a branch and I'm not sure if this is affecting whether this works or not. 我的文本编辑器在说If语句缺少分支,并且我不确定这是否会影响它的工作。

 var userNames = {Tom:"PassW0rd", Bill:"Apples40C0lander", Ryan:"M1911p4ck4ge"}; var Greeting = "Insert Username:"; var i = 0; while (i < 3) { var enteredUserName = prompt(Greeting); var enteredPassword = prompt("enter password for " + enteredUserName); if ((userNames.hasOwnProperty(enteredUserName)) && (userNames.enteredUserName==enteredPassword)) { alert("Welcome"); break; } else { alert("incorrect Username or Password"); alert(""); window.open('', '_self', ''); window.close(); window.open('', '_self', ''); window.close(); i++ }} 

A simple change is enough here : userNames[enteredUserName] . 一个简单的更改就足够了: userNames[enteredUserName]
In JavaScript , Object can also accessed like array . JavaScriptObject也可以像array一样进行访问。
If you use userNames.enteredUserName , then it will check {enteredUserName:somevalue} (ie, enteredUserName will be considered as a String . but if you use it as userNames[enteredUserName] , then enteredUserName will be considered as variable and index to the Object . 如果使用userNames.enteredUserName ,它将检查{enteredUserName:somevalue} (即, enteredUserName将被视为String 。但是,如果您将其用作userNames[enteredUserName] ,则enteredUserName将被视为变量并指向Object索引。
Try below snippet. 请尝试以下代码段。

 var userNames = {Tom:"PassW0rd", Bill:"Apples40C0lander", Ryan:"M1911p4ck4ge"}; var Greeting = "Insert Username:"; var i = 0; while (i < 3) { var enteredUserName = prompt(Greeting); var enteredPassword = prompt("enter password for " + enteredUserName); if ((userNames.hasOwnProperty(enteredUserName)) && (userNames[enteredUserName]==enteredPassword)) { alert("Welcome"); break; } else { alert("incorrect Username or Password"); alert(""); window.open('', '_self', ''); window.close(); window.open('', '_self', ''); window.close(); i++ }} 

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

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