I need to create 3 objects using different methods. I then need to store those objects in an array. I then need to use iteration to display the results. I have kind of confused myself along the way. I get no results shown. Im not sure exactly whats wrong. I have a button onclick action so you would have to click the button to show the array....
did I create the array incorrectly? Am I accessing the array data incorrectly in the for loop?
html:
<p id="3"></p>
<button onclick="ObjectArray()">click me</button>
javascript:
function ObjectArray() {
// object literal
var id1 = {
firstName: "John",
lastName: "Doe",
id: "12345"
};
// keyword new
var id2 = new Object;
id2.firstName = "Adam";
id2.lastName = "Bakely";
id2.id = "abcdef";
// object constructor
function employee(first, last, id_num) {
this.firstName = first;
this.lastName = last;
this.id_num = id;
}
var id3 = new employee("joe", "john", "abc123");
//create an array
var IdArray = [id1, id2, id3];
//for loop to display results
var text="";
var i;
for (i = 0; x < IdArray.length; i++){
text += IdArray[i] + "<br>";
document.getElementById("3").innerHTML = text;
}
}
I was also wondering how I could access this array from another function for a future task.