Stack Overflow Asked by Maxene Graze on November 15, 2021
I’m trying to make a basic one-page address book without a database, and I wanted to assign ids to the contacts to make them easier to find. However, when I’ve tried to add contacts, nothing gets assigned. Any help would be appreciated!
function AddressBook() {
this.contacts = [];
this.currentId = 0;
}
AddressBook.prototype.addContact = function(contact) {
contact.id = this.assignID;
this.contacts.push(contact);
}
AddressBook.prototype.assignID = function() {
this.currentID += 1;
return this.currentId;
}
AddressBook.prototype.findContact = function(id) {
for (let i = 0; i < this.contacts.length; i++) {
if(this.contacts[i].id == id) { //uses loose equality to leave room for input error
return this.contacts[i];
}
};
return false;
}
function Contact(firstName, lastName, phoneNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
Contact.prototype.fullName = function() {
return this.firstName + " " + this.lastName;
}
In addition to calling assignId
note that you have uppercase D in this.currentID += 1
but lowercase d in the line below that and in the constructor so you should make the uppercase D lowercase
Answered by jason_r on November 15, 2021
You have defined assignID
as a method, but you've used as a property.
To your code works, you need change addContact
method like the below code:
AddressBook.prototype.addContact = function(contact) {
contact.id = this.assignID(); //<===== calling like a method not a property
this.contacts.push(contact);
}
However, note that currentId
is a property of one instance. So, you code will ever put 1 to all IDs.
To fix it, you can change currentId
to be one class property like this:
AddressBook.currentId
And in all places reference this property with the class name before:
AddressBook.prototype.assignID = function() {
AddressBook.currentId += 1; //<====== note that in your code you use ID as upper case, different that you have defined it
return AddressBook.currentId;
}
Using currentId
as a class property, all instances of the class will share this property and each call for assignID()
will update the shared property and make that the next instance of the class gets one new ID.
The final code should looks like:
function AddressBook() {
this.contacts = [];
}
AddressBook.currentId = 0;
AddressBook.prototype.addContact = function(contact) {
contact.id = this.assignID();
this.contacts.push(contact);
}
AddressBook.prototype.assignID = function() {
return ++AddressBook.currentId; //here, first currentId is incremented and after it is returned
}
AddressBook.prototype.findContact = function(id) {
for (let i = 0; i < this.contacts.length; i++) {
if(this.contacts[i].id == id) { //uses loose equality to leave room for input error
return this.contacts[i];
}
};
return false;
}
function Contact(firstName, lastName, phoneNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
Contact.prototype.fullName = function() {
return this.firstName + " " + this.lastName;
}
Answered by Gean Ribeiro on November 15, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP