InsideDarkWeb.com

id not assigned in contact list

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

Stack Overflow Asked by Maxene Graze on November 15, 2021

2 Answers

2 Answers

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

Add your own answers!

Related Questions

Flutter text background with figure

1  Asked on December 1, 2020 by talisman7525

 

Minimum count to make equal Array

4  Asked on December 1, 2020

   

Trying to convert a csv to HDF5 and read it using vaex

1  Asked on December 1, 2020 by droning-hangman

       

Run Tree Structure in Python

1  Asked on November 30, 2020 by tianhe-xie

 

How iterate over a list and delete values along the way

1  Asked on November 30, 2020 by jaden-listrani-ayers

 

import packages from project directory

2  Asked on November 30, 2020 by will-waltrip

     

Removing duplicates while sorting numbers inside a String in java

5  Asked on November 30, 2020 by swetha-haridoss

       

pointers cant read the correct elements in array

2  Asked on November 29, 2020 by jabou

   

pandas does not load the sub packages properly

0  Asked on November 29, 2020 by realbro

   

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.