Advertisement
kstoyanov

02. Registration js exam

Aug 13th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(arg) {
  2.   const numInput = Number(arg.shift());
  3.   let sucRegCount = 0;
  4.   const pat = /(U\$)(?<userName>[A-Z][a-z]{2,})\1(P@\$)(?<password>[A-Za-z]{5,}\d+)\3/g;
  5.  
  6.   for (let i = 0; i < numInput; i++) {
  7.     const str = arg[i];
  8.     const isValid = str.match(pat);
  9.  
  10.     if (isValid) {
  11.       const regExResult = pat.exec(str);
  12.       const { userName, password } = regExResult.groups;
  13.       sucRegCount += 1;
  14.       console.log('Registration was successful');
  15.       console.log(`Username: ${userName}, Password: ${password}`);
  16.     } else {
  17.       console.log('Invalid username or password');
  18.     }
  19.   }
  20.  
  21.   console.log(`Successful registrations: ${sucRegCount}`);
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement