Array Log Starts with Undefined

I made this code for the Reversing an array" exercise but it always starts with undefined and I can’t figure out why.

<head>
    <meta charset="utf-8">
  <!-- Simply adds JQuery -->
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<!-- This is the form for user input-->
  <form action="/action_page.php">
   Item to add: <input type="text" id="item"><br>
   <input type="submit" value="Add To List" id="submit">
   <input type="submit" value="Submit" id="enter">
 </form>
 <!-- Some Specfications for the user -->
 <h3>No Duplicate Items Plz</h3>
 <h4>Answer Appears in Console</h4>


<script>

  //These are the variables I'm using. I bet you can figure out what they do
  var arrayOfThing = ["hi", "bye", "Good Luck"];
  var reversedArray = [];
  var item = $('#item').val();

  //The following function reverses an array specified with the "arrayOfthing" and "a" variables
  function reverseArrayToNew() {
  // Variable "a" is the length of the array
    var a = arrayOfThing.length;
    reversedArray = [];
    // I made sure to remove 1 from "a" as arrays start at 0 not 1
    for (a - 1; a >= 0; a = a - 1) {
      reversedArray.push(arrayOfThing[a])
    //  console.log(arrayOfThing[a])
      }
    console.log(reversedArray);
  }
//The following function occours when user adds and item
$("#submit").click(function(){
  arrayOfThing.push($('#item').val())
});
//The following function occours when user wishes to reverse an array
$("#enter").click(function(){
  reverseArrayToNew()
//  console.log("Submit Button Has Been Clicked");

});
</script>

</body>
</html>

Do you have any idea what is going wrong? I keep trying different methods and it comes back the same. :slight_smile:
I also can’t figure out how to do duplicate items.

Undefined means that this not exists.

If you look var arrayOfThing = [“hi”, “bye”, “Good Luck”]; there are 3 elements [0, 1, 2]
var a = arrayOfThing.length; this give you 3.

but your for loop goes 0, 1, 2, 3 (because arrayOfThing.length) so 4th element does not exists and this is why you get undefined. When your loop will go only 0, 1, 2 then you will not have undefined and will work. I think you should get solution in a second :wink:

And for future normally we use in for loop variable i don’t know why but every book have i
for(i = 0, i < HERE_IS_LENGTH_OF_ARRAY; i++){
}

elements go 0,1,2,3,4

or

for(i = HERE_IS_LENGTH_OF_ARRAY; i >= 0; i–){
}

elements go 4,3,2,1,0

because your variable a is now very very weird

In my original post I stated:

}

Wouldn’t that fix the issue? I also wrote: “a <= 0” so that it wouldn’t stop at 1.
Also, it wouldn’t be ++ as we are going in reverse order.

Your for loop starts with ‘a - 1’. This part is supposed to contain the declaration of the variable the loop revolves around.
You have ‘var a = arrayOfThing.length;’ a few lines earlier. You can just remove it from where it is and replace the ‘a - 1’ with it, or just remove the ‘a - 1’ and leave it blank (just make sure you leave the ; before ‘a >= 0’).