Reputation: 279
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>
Upvotes: 1
Views: 75
Reputation: 9
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");
}
Upvotes: 0
Reputation: 2404
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.
Upvotes: 1
Reputation: 51
You can access each element by its index
if(letters[index]=='a') {
document.write("Yes it is true");
}
Upvotes: 0
Reputation: 33399
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.
Upvotes: 2
Reputation: 279
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
Upvotes: -2
Reputation: 3386
you haven't define any varible with name a. you should use 'a' for checking in your aray
Upvotes: 0
Reputation: 2361
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>
Upvotes: 0