Currently, I am working on creating an address book using JavaScript. The problem I am facing is that the properties of my objects are not providing me with the necessary information. I need 4 different properties instead of 4 loops with the same property.
The for loop is creating a loop of 4 li
list items based on the information stored in the objects.
However, all I am getting is EMAIL repeated in each loop.
I hope you understand the issue and can offer your assistance. Below is the code snippet:
// Contact list function
function Contact(fname, lname, address, email) {
this.fname = fname;
this.lname = lname;
this.address = address;
this.email = email;
}
// Contacts
var tony = new Contact("Tony", "Stark", "Avengers 123", "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e37703f3370372c3130333f301e36312a333f3732703d3133">[email protected]</a>");
var steve = new Contact("Steve", "Rogers", "Avengers 12", "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e5e7f6a8e7ebe3f4efe5e7c6eee9f2ebe7efeaa8e5e9eb">[email protected]</a>");
// All contacts
var contacts = [tony, steve];
// Appending the objects
var body = document.getElementsByTagName('body')[0];
var ul = document.createElement('ul');
body.appendChild(ul);
function theContacts() {
var li = document.createElement('li');
li.innerHTML = tony.fname + ' ' + stark.lname;
ul.appendChild(li);
for (i = 0; i < 4; i++) {
li = document.createElement('li');
for (var key in tony) {
li.innerHTML = key;
}
ul.appendChild(li);
}
}
// Calling the object
theContacts();
Thank you for your support!