Simple Javascript doesn't return anything -
i'm trying learn javascript. i've written code should return a, b, c, d , if letter in var letters, should alert message "yes true".
when run code doesn't return me anything.
can 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 true"); } } </script>
a variable have not defined, dont confused "a" (string)
and letters array. x index in array iterating through. letters[x] specific element index x.
so, "a" == letters[x] compare fix code.
as side note, can suggest better way of solving problem.
you trying solve iterating through whole list , comparing elements. ok, if want learn concepts iteration, thiscould done lot easier inbuild methods in string object, indexof in 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 true"); } </script>
indexof returns index in array of element pass argument, -1 if not in array. if number bigger or equal 0 means element exist in array.
Comments
Post a Comment