简体   繁体   English

简单的Javascript不返回任何内容

[英]Simple Javascript doesn't return anything

I'm trying to learn some Javascript. 我正在尝试学习一些Javascript。 I've written this code where it should return a, b, c, d and if the letter a is in the var letters, it should alert the message "Yes it is true". 我已经编写了此代码,该代码应返回a,b,c,d,并且如果字母a在var字母中,则应提醒消息“是的,这是真的”。
When I run the code it doesn't return me anything. 当我运行代码时,它什么也不会返回。

Can anyone see why? 谁能看到原因?

<script type="text/javascript">

    var letters = ["a", "b", "c", "d"]
    var numbers = ["one", "two", "three", "four"]

    for(x=0; x < letters.length; x++) {
        document.write(letters);
        if(a in letters)  {
            document.write("Yes it is true");
        }
    }

</script>

Instead of a in letters , you should use letters.indexOf('a') > -1 . 您应该使用letters.indexOf('a') > -1代替a in letters What's the difference? 有什么不同?

To start with, a on its own is a variable. 首先, a本身就是一个变量。 You have not defined this variable. 您尚未定义此变量。 Secondly, in does not search values , only keys . 其次, in不搜索值中 ,仅搜索 The keys of this array are 0,1,2,3. 该数组的键是0、1、2、3。 indexOf , on the other hand, does search values. 另一方面, indexOf会搜索值。

Additionally, you don't need a for loop for this. 另外,您不需要为此的for循环。

a is a variable that you have not defined, dont get confused with "a" (string) a是您尚未定义的变量,请不要与“ a” (字符串)混淆

And letters is the array. 字母是数组。 x is the index in the array that you are iterating through. x是您要遍历的数组中的索引。 letters[x] is an specific element with index x. letter [x]是索引为x的特定元素。

So, you could do "a" == letters[x] to compare to fix your code. 因此,您可以执行“ a” == letters [x]进行比较以修复您的代码。

As a side note, I can suggest a better way of solving your problem. 作为附带说明,我可以建议一种解决您的问题的更好方法。

You are trying to solve by iterating through the whole list and comparing the elements. 您正在尝试通过遍历整个列表并比较元素来解决问题。 That is ok, if you want to learn concepts like iteration, but thiscould be done a lot easier with inbuild methods in the string object, indexOf in this case. 没关系,如果您想学习迭代等概念,但是使用字符串对象(在本例中为indexOf)中的内置方法可以轻松得多。

<script type="text/javascript">

    var letters = ["a", "b", "c", "d"]
    var numbers = ["one", "two", "three", "four"]

    if(letters.indexOf("a") >= 0)  {
        document.write("Yes it is true");
    }

</script>

indexOf returns the index in the array of the element that you pass as argument, -1 if it is not in the array. indexOf返回您作为参数传递的元素的数组中的索引,如果不在数组中,则返回-1。 So if this number is bigger or equal than zero means that the element exist in the array. 因此,如果此数字大于或等于零,则表示该元素存在于数组中。

please review this code, a is element of letters array 请查看此代码,其中a是letters数组的元素

<script type="text/javascript">

    var letters = ["a", "b", "c", "d"]
    var numbers = ["one", "two", "three", "four"]

    for(x=0; x < letters.length; x++) {
        document.write(letters);
        if(letters[x]=='a')  {
            document.write("Yes it is true");
        }
    }

</script>

you haven't define any varible with name a. 您尚未定义任何名称为a的变量。 you should use 'a' for checking in your aray 您应该使用'a'签入您的ray

You can access each element by its index 您可以按其索引访问每个元素



    if(letters[index]=='a')  {
        document.write("Yes it is true");
    }

You can use letters.indexOf(a) instead of your for loop, it will returns the index of the variable 'a' inside the array 'letters' (-1 if not found). 您可以使用letters.indexOf(a)代替for循环,它将返回数组“字母”内的变量“ a”的索引(如果未找到则为-1)。 But before that you need to define the variable 'a'. 但是在此之前,您需要定义变量“ a”。

If you really want to keep the for loop, you should use it like this : 如果您真的想保留for循环,则应使用它:

var a="a";
for(x=0; x < letters.length; x++) {
        document.write(letters);
        if(letters[x]==a)  {
            document.write("Yes it is true");
        }

Answer: 回答:

for(x=0; x < letters.length; x++) {
        document.write(letters + "<br/>");
        if(a = letters)  {
            document.write("Yes it is true" + "<br/>");
        }
    }

Printout: a,b,c,d Yes it is true a,b,c,d Yes it is true a,b,c,d Yes it is true a,b,c,d Yes it is true 打印输出:a,b,c,d是,这是真的a,b,c,d是,这是真的a,b,c,d是,这是真的a,b,c,d是,这是真的

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

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