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.
new Contact("Carole", "Lévisse");<-- this returns an empty object. (Well, not empty, butnomandprenomaren't set)