1

I'm trying to learn some 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".
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>

7 Answers 7

2

Instead of a in letters, you should use letters.indexOf('a') > -1. What's the difference?

To start with, a on its own is a variable. You have not defined this variable. Secondly, in does not search values, only keys. The keys of this array are 0,1,2,3. indexOf, on the other hand, does search values.

Additionally, you don't need a for loop for this.

Sign up to request clarification or add additional context in comments.

Comments

1

a is a variable that you have not defined, dont get confused with "a" (string)

And letters is the array. x is the index in the array that you are iterating through. letters[x] is an specific element with index x.

So, you could do "a" == letters[x] to compare to fix your code.

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.

<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. So if this number is bigger or equal than zero means that the element exist in the array.

Comments

0

please review this code, a is element of letters array

<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>

Comments

0

you haven't define any varible with name a. you should use 'a' for checking in your aray

2 Comments

ah didn't see your comment :-) Thanks a lot
Using in is the wrong way to go anyways. in checks keys. See my answer.
0

You can access each element by its index



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

Comments

0

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). But before that you need to define the variable 'a'.

If you really want to keep the for loop, you should use it like this :

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

Comments

-2

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

1 Comment

You're setting a variable, not comparing a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.