Advertisement
TZinovieva

Fancy Barcodes JS Fundamentals

Mar 29th, 2023
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fancyBarcodes(array) {
  2.     let codesNumber = array.shift();
  3.     let regex = /@#+(?<barcode>[A-Z][a-zA-Z0-9]{4,}[A-Z])@#/;
  4.  
  5.     for (let i = 0; i < codesNumber; i++) {
  6.         let string = regex.exec(array[i]);
  7.  
  8.         if (string !== null) {
  9.             let valid = string.groups.barcode;
  10.  
  11.             let productGroup = ``;
  12.             for (let currSymbol of valid) {
  13.  
  14.                 if (isNaN(currSymbol) === false) {
  15.                     productGroup += currSymbol;
  16.                 }
  17.             }
  18.             if (productGroup == ``) {
  19.                 productGroup = `00`;
  20.             }
  21.             console.log(`Product group: ${productGroup}`);
  22.         } else {
  23.             console.log("Invalid barcode");
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement