Advertisement
maymunskoa

Winning Ticket (RegExp)

Mar 20th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   let data = input[0].split(/[,\s]+/g);
  3.   let reg = /^.*?(?<matchgroup>\@{6,}|\${6,}|\^{6,}|\#{6,}).*?$/;
  4.  
  5.  
  6.   for (const line of data) {
  7.     let noMatch = false;
  8.     if (line.length === 20) {
  9.     let left = line.substring(0, 10);
  10.     let right = line.substring(10);
  11.     if(left.match(reg) !== null && right.match(reg) !== null){
  12.         if(left.match(reg).groups.matchgroup[0] === right.match(reg).groups.matchgroup[0]){
  13.             let length =
  14.             left.match(reg).groups.matchgroup.length < right.match(reg).groups.matchgroup.length ?
  15.             left.match(reg).groups.matchgroup.length  : right.match(reg).groups.matchgroup.length ;
  16.             let symbol = left.match(reg).groups.matchgroup[0];
  17.             let jackpot = length === 10? ' Jackpot!': '';
  18.             console.log(`ticket "${line}" - ${length}${symbol}${jackpot}`);
  19.            
  20.         }else{
  21.             noMatch = true;
  22.         }
  23.        
  24.     }else{
  25.         noMatch = true;
  26.     }
  27.     if(noMatch){
  28.         console.log(`ticket "${line}" - no match`);
  29.        
  30.     }
  31.        
  32.     } else {
  33.       console.log(`invalid ticket`);
  34.     }
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement