Advertisement
AreWe

WinningTicket 100/100 Fixed

Apr 6th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. winningTicket = ([input]) => {
  2.     let patternRepeat = /([@#\$\^])\1{5,9}/g;
  3.     input = input.split(/[\s,]+/).forEach(ticket => {
  4.         if(ticket.length === 20) {
  5.             let left = ticket.substr(0, 10), right = ticket.substr(10);
  6.             let leftRepeat = left.match(patternRepeat), rightRepeat = right.match(patternRepeat);
  7.             if(leftRepeat !== null && rightRepeat !== null && leftRepeat[0][0] === rightRepeat[0][0]) {
  8.                 leftRepeat = leftRepeat[0];
  9.                 rightRepeat = rightRepeat[0];
  10.                 console.log(`ticket "${ticket}" - ${Math.min(rightRepeat.length, leftRepeat.length)}${rightRepeat[0]}${rightRepeat.length === 10 && leftRepeat.length === 10 ? ' Jackpot!': ''}`);
  11.             } else {
  12.                 console.log(`ticket "${ticket}" - no match`);
  13.             }
  14.         } else {
  15.             console.log('invalid ticket');
  16.         }
  17.     });
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement