Javascript 理解数组中的循环

Javascript 理解数组中的循环,javascript,arrays,loops,Javascript,Arrays,Loops,试图理解这一点以及它是如何工作的。基本上,目标是让用户输入密码以“获得访问权限”。这是代码,但我正试图准确地理解它 两个问题真的是: 1.为什么输出首先被声明为无访问权?如果不起作用,为什么不是一个else? 2.如果有人能解释I=0,我很困惑整个循环函数是如何工作的;userPassword.length>i;我++ function btnCheckCode_onclick() { // assign textbox elements to variables for easier

试图理解这一点以及它是如何工作的。基本上,目标是让用户输入密码以“获得访问权限”。这是代码,但我正试图准确地理解它

两个问题真的是: 1.为什么输出首先被声明为无访问权?如果不起作用,为什么不是一个else? 2.如果有人能解释I=0,我很困惑整个循环函数是如何工作的;userPassword.length>i;我++

function btnCheckCode_onclick()
{
    // assign textbox elements to variables for easier access
    var userInputTextbox = document.getElementById("txtCode");
    var outputTextbox = document.getElementById("txtOutput");

    var userInput = userInputTextbox.value
    var userPassword = ["Admin", "Secret", "Letmein", "abc123", "qwerty"];
    var output = "Sorry, No Access";

    for(i = 0; userPassword.length > i ; i++ )
    {
        if(userPassword[i] == userInput) 
        {
            output = "Welcome Privileged User";
        }

    }

    userInputTextbox.value = userInput
    outputTextbox.value = output
}

此for指令是一个循环,它遍历可能的用户密码列表,并将其与输入的密码进行比较,如果存在匹配项,则会更改输出:

//将文本框元素分配给变量以便于访问
var checkPassword=函数(){
var userInputTextbox=document.getElementById(“txtCode”);
var outputTextbox=document.getElementById(“txtOutput”);
var userInput=userinputextbox.value;
var userPassword=[“Admin”、“Secret”、“Letmein”、“abc123”、“qwerty”];
var output=“对不起,没有访问权限”;
对于(i=0;i


检查
它在密码数组中循环,查看是否与给定密码匹配

  • 它使用I作为计数器,并将其初始化为0

  • 下一部分在每次循环运行时进行评估,以检查循环是否应继续。在这种情况下,只要i小于密码长度,它就会继续

  • 每次运行循环时,i++部分都会增加循环计数器


话虽如此,请不要在任何地方使用此代码。检查密码不是您应该在客户端执行的操作。每个人都可以看到此代码,绕过此检查非常简单。

输出的值设置在循环之前:

var output = "Sorry, No Access";
只有在循环内的条件

userPassword[i] == userInput
计算结果为
true
。因此,第一个问题的答案是,您自己设置特定的初始值

关于第二个问题,循环构造的参数:

i = 0; i < userPassword.length; i++ 
i=0;i
定义如下:

variable = initial value; variable < limit; increment variable after iteration
变量=初始值;变量<极限;迭代后的增量变量
换句话说,您声明变量
i
,并将其设置为
0
;只要
i
的值小于限制值,或者特别是
userPassword
数组的长度,您就告诉解释器进行迭代;在每次迭代后,将
1
添加到
i
的值中


在第一个循环迭代
i=0
内,然后是
i=1
。由于
userPassword
的长度为
5
,因此循环将继续。在第二次迭代之后,
i==2
,循环一直持续到
i==5
。由于
userPassword
的5个索引被标识为
[0,1,2,3,4]
,因此这非常好。

for循环正在userPassword数组中迭代。阵列的长度现在是5,但将来可能会改变。使用数组的长度作为条件的一部分意味着如果使数组变长或变短,则不必更改循环

对于循环,您需要一个充当迭代器的变量:
i
。数组中的项的索引范围为0到1,小于数组的总长度。您希望迭代器
i
的值从数组的开头0开始,一直到最后

在您的例子中,循环的结尾也是数组的结尾,数组由您的条件定义:
userPassword.length>i

当您有类似于
userPassword[i]
的内容时,它表示数组在该索引处保存的值--
i
将在循环期间被一个整数替换。在循环中,一个索引一个索引地迭代,并根据用户输入的索引检查数组中的值,直到迭代器达到条件语句定义的限制

对于第一个问题,通常在函数的开头初始化每个变量<代码>输出
可以是函数运行之前的任何内容,但通过使用消息初始化
输出
,您不会有试图处理未定义变量的风险

  • 因为如果用户没有键入任何字符串“Admin”、“Secret”、“Letmein”、“abc123”、“qwerty”,则输出不需要在之后设置为“对不起,没有访问权”,因为这是默认值

  • i=0:是i的起始值,userPassword.length>i userPassword.length是数组中的元素数=>5 i++意味着在每次迭代后将i设置为i+1

  • 循环只检查用户输入是否与userPassword数组中的一个字符串匹配,如果匹配,则将输出设置为“欢迎特权用户”。 问题是,如果用户键入“Admin”,循环也会检查其他四个值

    userPassword.some(password => {
      if(password == userInput){
         output = "Welcome Privileged User";
      }
      return password == userInput;
    });
    
    我添加了else,现在让我们检查变体:

    0 iteration: true -> output = "Welcome Privileged User";
    1 iteration: false -> output = "Sorry, No Access";
    2 iteration: false -> output = "Sorry, No Access";
    3 iteration: false -> output = "Sorry, No Access";
    4 iteration: false -> output = "Sorry, No Access";
    
    所以,output=“对不起,没有访问权限”(但我们有真正的迭代)。这是因为如果条件为
    false
    ,else语句将重写变量输出

    当我们在for循环之前定义输出时,只有当条件
    if(userPassword[i]==userInput)
    返回
    true
    时,输出才能更改值。所以,这个变体是正确的

    关于循环的。for循环通常是创建循环时使用的工具

    for循环具有以下语法:

    for (statement 1; statement 2; statement 3) {
        code block to be executed
    }
    
    就你而言:

     for(i = 0; userPassword.length > i ; i++ ) {
            code block to be executed
        }
    
    语句1在循环开始之前设置变量(变量i=0)

    语句2定义循环运行的条件(userPassword.len)
     for(i = 0; userPassword.length > i ; i++ ) {
            code block to be executed
        }
    
       if(userPassword[i] == userInput) {
          output = "Welcome Privileged User";
        }
    
    if(userPassword[0] == userInput) => if("Admin" == userInput)
    if(userPassword[1] == userInput) => if("Secret" == userInput)
    if(userPassword[2] == userInput) => if("Letmein" == userInput)
    if(userPassword[3] == userInput) => if("abc123" == userInput)
    if(userPassword[4] == userInput) => if("qwerty" == userInput)
    
     for(i = 0; i < userPassword.length; i++)