I have to create an HTML page following certain guidelines. I am not asking anyone to do my homework for me but I am stuck on the first part.
Create and Initialize three arrays. Use for-loops to populate those arrays. Add an element to the end of one array.
I have created 2 arrays and used a pop element. But when I open the .htm it populates correctly but when pressing create it does the second part of my code. How do I separate the scripts?
<!DOCTYPE HTML>
<html>
<head>
<title>Testing JavaScript for Loop</title>
</head>
<body>
<p>The pop method removes the last element from an array.</p>
<button onclick="myFunction()">Create</button>
<button onclick="pop()">Pop</button>
<p id="demo"></p>
<script>
var fruits = ["Banana", " Orange", " Apple", " Mango"];
var newFruits = [];
document.getElementById("demo").innerHTML = newFruits;
function myFunction() {
for (var i in fruits) {
newFruits.push(fruits[i])
}
document.getElementById("demo").innerHTML = newFruits;
}
function pop() {
newFruits.pop();
document.getElementById("demo").innerHTML = newFruits;
}
</script>
<br>
<p>This one needs to add and element to the beginning.</p>
<button onclick="myFunction()">Create</button>
<button onclick="pop()">This button needs to add to beginning</button>
<p id="veggie"></p>
<script>
var veggies = ["Broccoli", " Carrots", " Cucumber", " Beans"];
var newVeggies = [];
document.getElementById("veggie").innerHTML = newVeggies;
function myFunction() {
for (var i in veggies) {
newVeggies.push(veggies[i])
}
document.getElementById("veggie").innerHTML = newVeggies;
}
/*need this to put an element at the beginning of the list */
function pop() {
newVeggies.pop();
document.getElementById("veggie").innerHTML = newVeggies;
}
</script>
</body>
</html>