Професионална програма
EvgeniJD avatar EvgeniJD 35 Точки

Star Enigma / RegExp Exercices


Някой може ли да ме насочи към решаването на въпросната задача със 100/100 (аз стигнах до 80/100) и евентуално да ми каже къде греша. Ще съм ви изключително благодарен! Решението ми: https://pastebin.com/wjvWvPia

Задачата : 

1.Star Enigma

The war is in its peak, but you, young Padawan, can turn the tides with your programming skills. You are tasked to create a program to decrypt the messages of The Order and prevent the death of hundreds of lives.

You will receive several messages, which are encrypted using the legendary star enigma. You should decrypt the messages, following these rules:

To properly decrypt a message, you should count all the letters [s, t, a, r]case insensitive and remove the count from the current ASCII value of each symbol of the encrypted message.

After decryption:

Each message should have a planet name, population, attack type ('A', as attack or 'D', as destruction) and soldier count.

The planet name starts after '@' and contains only letters from the Latin alphabet.

The planet population starts after ':' and is an Integer;

The attack type may be "A"(attack) or "D"(destruction) and must be surrounded by "!" (exclamation mark).

The soldier count starts after "->" and should be an Integer.

The order in the message should be: planet name -> planet population -> attack type -> soldier count. Each part can be separated from the others by any character except: '@', '-', '!', ':' and '>'.

Input / Constraints

  • The first line holds n – the number of messagesinteger in range [1…100];
  • On the next n lines, you will be receiving encrypted messages.


After decrypting all messages, you should print the decrypted information in the following format:

First print the attacked planets, then the destroyed planets.
"Attacked planets: {attackedPlanetsCount}"
"-> {planetName}"
"Destroyed planets: {destroyedPlanetsCount}"
"-> {planetName}"

The planets should be ordered by name alphabetically.








Attacked planets: 1

-> Alderaa

Destroyed planets: 1

-> Cantonica

We receive two messages, to decrypt them we calculate the key:

First message has decryption key 3. So we substract from each characters code 3.


The second message has key 5.


Both messages are valid and they contain planet, population, attack type and soldiers count.

After decrypting all messages we print each planet according the format given.








Attacked planets: 0

Destroyed planets: 2

-> Cantonica

-> Coruscant

We receive three messages.

Message one is decrypted with key 4:


Message two is decrypted with key 7:


This is invalid message, missing soldier count, so we continue.

The third message has key 5.


"It’s a trap!" – Admiral Ackbar

JavaScript Fundamentals 23/03/2020 13:27:53
willystyle avatar willystyle 2420 Точки
Best Answer

ред 10 го направи с безопасно взимане на стойност:

let key = encryptedMessage.match(keyPattern) && encryptedMessage.match(keyPattern).length;

EvgeniJD avatar EvgeniJD 35 Точки

Проверих дали key !== null, след което промених стойността на key = key.length . Трябва да се науча да проверявам всяка променлива свързана с regExp за null. Не знам дали това имаше предвид, но получих 100/100! Благодаря ти ОТНОВО!!!

J.Encheva avatar J.Encheva 1 Точки

и аз забих на абсолютно същото :D много благодаря !