kstoyanov

02. Fancy Barcodes js exam

Aug 8th, 2020 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   const input = args.slice(1);
  3.   const pattern = /^(?<start>@#+)(?<barcode>[A-Z][A-Za-z0-9]{4,}[A-Z])(?<end>@#+)$/;
  4.   input.forEach((line) => {
  5.     let code = '';
  6.     const match = pattern.exec(line);
  7.     if (match) {
  8.       for (const el of match.groups.barcode) {
  9.         if (el.charCodeAt() >= 48 && el.charCodeAt() <= 57) {
  10.           code += el;
  11.         }
  12.       }
  13.       code === '' ? console.log('Product group: 00') : console.log(`Product group: ${code}`);
  14.     } else {
  15.       console.log('Invalid barcode');
  16.     }
  17.   });
  18. }
Add Comment
Please, Sign In to add comment