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.
I also can’t figure out how to do duplicate items.