Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function starEnigma(arr) {
- let decryption = /[star]/gi
- let planetRegex = /@(?<name>[A-Za-z]+)[^@!:>\-]*:(?<population>[0-9]+)[^@!:>\-]*!(?<attackType>[A|D])![^@!:>\-]*->(?<soldierCount>[0-9]+)/g
- let numberOfMessages = arr.shift()
- let newString = ""
- let attackedPlanets = []
- let destroyedPlanets = []
- arr.forEach((message, index) => {
- if (index < numberOfMessages) {
- if(message.match(decryption)){
- match= message.match(decryption).length
- }else{
- match = 0
- }
- for(let letter of message){
- let letterCharCode = letter.charCodeAt(0) - match
- newString += String.fromCharCode(letterCharCode)
- }
- let planet = planetRegex.exec(newString)
- // newString = ""
- // if(planet==null){
- // continue
- // }
- if(planet){
- let planetName = planet.groups.name
- let planetType = planet.groups.attackType
- if (planetType === "A") {
- attackedPlanets.push(planetName)
- } else if(planetType === "D") {
- destroyedPlanets.push(planetName)
- }
- }
- }
- })
- console.log(`Attacked planets: ${attackedPlanets.length}`);
- if(attackedPlanets.length>0 && attackedPlanets.length!= undefined){
- logPlanets(attackedPlanets)
- }
- console.log(`Destroyed planets: ${destroyedPlanets.length}`);
- if(destroyedPlanets.length>0&& destroyedPlanets.length!= undefined){
- logPlanets(destroyedPlanets)
- }
- function logPlanets(arr) {
- arr.sort((a, b) => a.localeCompare(b)).forEach(planet=> {
- console.log(`-> ${planet}`);
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement