0

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;
}

2 Answers2

0

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;
}
Gean Ribeiro
  • 1,025
  • 2
  • 10
  • 23
  • I don't think you need to make `currentId` a class property since you can call `addContact` on a single `AddressBook` instance multiple times to add multiple contacts which would still all get unique IDs – acran Jul 21 '20 at 23:39
  • Yes @ecran ... I think that you have reason. But, maybe the better is to put currentId in Contact and not in AddressBook. So, if you have two instances of AddressBook you do not repeat the contact Id. – Gean Ribeiro Jul 21 '20 at 23:50
0

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

jason_r
  • 345
  • 1
  • 11