Answer the question
In order to leave comments, you need to log in
How to implement adding an object of some class with its properties to an array?
Hello! I have this telegram bot idea written in nodeJS that asks the user for the number of drops/pills, then asks for their name. The polling process takes place between Wizard scenes. Further, according to my idea, the bot should ask the following questions about each drug:
-How many days will you take the drug?
- How many times a day will you take the drug?
-What is the reception interval?
I did the process of polling the names of drugs. But I was faced with the problem of how to now enter the properties that I cited above into array objects.
Below I show the implementation of a survey about the number and names.
function PillsKeeper(pillsNumber) {
const totalPills = pillsNumber;
this.pillsArray = [];
this.addPills = function(pillName) {
if (this.pillsArray.length < totalPills) {
this.pillsArray.push(pillName);
}
console.log(`${this.pillsArray.length} / ${totalPills}, ${this.pillsArray[this.pillsArray.length - 1]}`);
return this;
}
this.showPills = function() {
return this.pillsArray;
}
return this; //pattern цепочка вызовов
}
let pillsKeeper;
const medsHandler = Telegraf.on('text', async ctx => {
if (!pillsKeeper) {
pillsKeeper = new PillsKeeper(pillsNumberTotal);
}
pillsKeeper.addPills(ctx.message.text);
await pillsKeeper.showPills().length === pillsNumberTotal
? (ctx.reply(`Всего введено препаратов: ${pillsKeeper.showPills().join(', ')}`), ctx.wizard.next())
: ctx.reply('Отличный препарат!!')
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question