Advertisement
svephoto

Winning Ticket [JavaScript]

Feb 11th, 2022 (edited)
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function winningTicket(input) {
  2.   let tickets = input.split(/[\s]*[,][\s]*/g);
  3.   let pattern = /([\#\@\$\^])\1{5,}/g;
  4.  
  5.   for (let ticket of tickets) {
  6.     if (ticket.length === 20) {
  7.       let leftPart = ticket.substring(0, ticket.length / 2);
  8.       let rightPart = ticket.substring(ticket.length / 2);
  9.      
  10.       let leftMatch = leftPart.match(pattern);
  11.       let rightMatch = rightPart.match(pattern);
  12.      
  13.       if (leftMatch != null && rightMatch != null) {
  14.         leftMatch = leftMatch.join('').toString();
  15.         rightMatch = rightMatch.join('').toString();
  16.  
  17.         let minLength = Math.min(leftMatch.length, rightMatch.length);
  18.        
  19.         if (leftMatch && rightMatch) {
  20.           let winFirstHalf = leftMatch.substring(0, minLength);
  21.           let winSecondHalf = rightMatch.substring(0, minLength);
  22.  
  23.           if (winFirstHalf === winSecondHalf) {
  24.             if (winFirstHalf.length == 10) {
  25.               console.log(`ticket "${ticket}" - ${minLength}${winFirstHalf.substring(0, 1)} Jackpot!`);
  26.             } else {
  27.               console.log(`ticket "${ticket}" - ${minLength}${winFirstHalf.substring(0, 1)}`);
  28.             }
  29.           }
  30.         }
  31.       } else {
  32.         console.log(`ticket "${ticket}" - no match`);
  33.       }
  34.     } else {
  35.       console.log(`invalid ticket`);
  36.     }
  37.   }
  38. }
  39.  
  40. // winningTicket("Cash$$$$$$Ca$$$$$$sh");
  41. // winningTicket("$$$$$$$$$$$$$$$$$$$$, aabb  , th@@@@@@eemo@@@@@@ey");
  42. // winningTicket("validticketnomatch:(");
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement