0

Here I have used destructuring to extract the data from a message. I need to then use it outside of its scope. I duplicated the same variables/constants to made it exclusively outside of the scope. How can I avoid these duplicate variables and use the restructured variables/constants directly in the code?

let OTP2, amount2, account2, payee2;
let channel;
switch (sender) {
    case 'SBIINB':
        const regexSBINetBankingFundTransfer = /.*?OTP.*?Rs. (?<amount>\d+) .*?(?<account>\d+)\s+to\s+(?<payee>.*?)\s+is\s(?<OTP>\d+)/m;
        if(regexSBINetBankingFundTransfer.test(message)) {
            let { groups: { account, amount, payee, OTP } } = regexSBINetBankingFundTransfer.exec(message);
            account2 = account; amount2 = amount; payee2 = payee;
            notificationType = 'fundTransfer';
        } else {
            const regexSBINetBankingLogin = /.*OTP.*(?<OTP>\d{8}).*/m;
            if(regexSBINetBankingLogin.test(message)) {
                let { groups: { OTP } } = regexSBINetBankingLogin.exec(message);
                if (OTP != null) {
                    notificationType = 'login';
                    OTP2 = OTP;
                } else {
                    notificationType = 'uncategorized';
                }
            }
        }

Edit: The assignment without declaration strategy didn't work

enter image description here

SenG
  • 713
  • 1
  • 7
  • 15
  • Sorry, didn't work. As you can see from the screenshot OTP variable is not available outside its scope. – SenG May 01 '21 at 02:14
  • Again sorry, it worked!!! In the above screenshot, line no.45 was hiding the OTP variable outside. Once I removed it, it was recognized. Thank you very much! – SenG May 01 '21 at 02:15
  • 1
    Glad it worked, you can mark this question as a duplicate - there should be an option for you on the question to say that the answer linked answers your question – Nick Parsons May 01 '21 at 02:42

1 Answers1

0

The assignment without declaration is the right solution.

enter image description here

SenG
  • 713
  • 1
  • 7
  • 15