Advertisement
Guest User

Untitled

a guest
Mar 20th, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function starEnigma(input) {
  2.     let attacked = [];
  3.     let destroyed = [];
  4.     let numberOfMessages = input.shift();
  5.  
  6.     for (let i = 0; i < numberOfMessages; i++) {
  7.         let message = input[i].toLowerCase();
  8.         let result = message.match(/[star]/g);
  9.         let newMessage = input[i]; //here
  10.         if (result !== null) {     //here
  11.             newMessage = input[i].split("").map(e => e.charCodeAt(0) - result.length).map(a => String.fromCharCode(a)).join("");
  12.         }                          //here
  13.         let regex = /@(?<name>[A-Z][a-z]+)[^@!\->:]*:(?<population>\d+)[^@!\->]*!(?<attack>[A|D])![^@!\->:]*->(?<solidercount>\d+)/g;
  14.         let matching = regex.exec(newMessage);
  15.  
  16.         if (matching !== null) {
  17.             if (matching.groups.attack === "D") {
  18.                 destroyed.push(matching.groups.name)
  19.             } else if (matching.groups.attack === "A")
  20.                 attacked.push(matching.groups.name)
  21.         }
  22.  
  23.     }
  24.  
  25.  
  26.     console.log(`Attacked planets: ${attacked.length}`);
  27.     if (attacked.length > 0) {
  28.         let sorted = attacked.sort((a, b) => a.localeCompare(b))
  29.         sorted.forEach(element => console.log(`-> ${element}`))
  30.     }
  31.     console.log(`Destroyed planets: ${destroyed.length}`);
  32.     if (destroyed.length > 0) {
  33.         let secondSorted = destroyed.sort((a, b) => a.localeCompare(b))
  34.         secondSorted.forEach(element => console.log(`-> ${element}`))
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement