0

I'm learning JavaScript and I have an exercice to do. Basically it is a script that displays a contact list and also (bonus requirement) gives a way to add some contacts in it. So my approach was to create an object/class Contact and a class ContactBook that would hold a list of contacts. It requires to be initialised with 2 specific contacts at launch time.

My initial problem is that the method afficheInfos() that should return values for the instance does return undefined values. I can't put the finger on why it does that, so I made a function debugContact() to focus on my Contact class.

I don't understand why the console.log() in the constructor does not display at all. It prevent me from debugging the code. I tried to find ways to debug the code and got on the keyword debugger but I actually don't see what it does and how it works. I did a similar code as part of the course (creating instances of a class and initialize it's data) which worked fine. I can't spot any particular differences, so I'm wondering what I'm missing here. This is the WIP code in it's current state bellow:

/* 
Activité : gestion des contacts
*/

// Définir un objet contact
class Contact {
   contructor(nom, prenom) {
      this.nom = nom;
      this.prenom = prenom;

      debugger;

      console.log(this);
      //console.log("Nouveau contact ajouté: "+this.toString());
   }

   afficheInfos() {
      return `Nom: ${this.nom}, Prénom: ${this.prenom}`;
   }
}

// Définir un livre de contact
class ContactBook {


   constructor() {
      const caroleLevisse = new Contact("Carole", "Lévisse");
      const melodieNelsonne = new Contact("Mélodie", "Nelsonne");
      this.tabContacts = [caroleLevisse,melodieNelsonne];
   }

   addContact() {
      const nom = prompt("Saisissez le nom: ");
      const prenom = prompt("Saisissez le prénom: ");

      const newContact = new Contact(nom,prenom);

      this.tabContacts.push(newContact);
   }

   displayContacts() {
      var i = 0;
      //this.tabContacts.forEach(item => console.log(item.toString()));
      this.tabContacts.forEach(item => console.log("Index ["+String(i++) + "] donne " + item.afficheInfos()));
      /*
      for (var contact of this.tabContacts) {
         console.log(contact.afficheInfos());
      }; //*/

      /*
      for (var i = 0 ; i < this.tabContacts.length;i++) {
         console.log("");
      } //*/
   }
}

function main() {
   contactBook = new ContactBook();

   programContinues = true;

   while (programContinues) {
      displayMenu();
      choice = prompt("Choisissez une option: "); //TODO coder la boucle du programme

      switch(choice) {
         case "1":
            contactBook.displayContacts();
            break;
         case "2":
            contactBook.addContact();
            break;
         default:
            programContinues = false;
      }
   }

   console.log("Fin du programme.")
}

function debugContact() {
   debugContinues = true;

   while (debugContinues) {
      choice = prompt("Continuer? ");

      switch (choice) {
         case "o":
            const unPrenom = prompt("Prénom: ");
            const unNom = prompt("Nom: ");
            var contactTest = new Contact(unNom,unPrenom);
            console.log(contactTest.afficheInfos());
            break;
         default:
            debugContinues = false;
      }
   }
}

function displayMenu() {
   console.log("1) Lister les contacts.");
   console.log("2) Ajouter un contact.");
   console.log("Autre) Quitter");
}

debugContact();

//main();

Could you help me understand why it is not working properly? Kind Regards.

4
  • No direct clue why, but your constructor does nothing: new Contact("Carole", "Lévisse"); <-- this returns an empty object. (Well, not empty, but nom and prenom aren't set) Commented Sep 9, 2019 at 8:25
  • 1
    @sjahan probably because it's not a constructor it's a contructor - no s. Commented Sep 9, 2019 at 8:29
  • Yeah, just saw that too :) Commented Sep 9, 2019 at 8:30
  • @sjahan. No it works. If I could upload a screenshot I could show you. Everything is working perfectly as expected. I'm a bit ashamed I did not spot this typo. Commented Sep 9, 2019 at 8:46

1 Answer 1

2

I didn't get why your constructor did nothing...

Easy: you got a typo contructor -> constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. I'm using Brackets and it highlighted the keywords the same ways so I implied the spelling was correct on both constructor keywords. I'm sorry I didn't have the presence of mind to check that simple fact. I had hints with basic code not running. Regards.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.