D
D
DeVit02022-02-14 21:49:10
Node.js
DeVit0, 2022-02-14 21:49:10

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('Отличный препарат!!')
})


And at this point I was stuck before the question, create a class, or write the same function. Help, who can.
I ask you not to throw stones, I'm just training!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question