Instructions for the challenge:
In 🎅 Santa's workshop, some Christmas messages have been written in a peculiar way: the words within the brackets must be read backwards.
Santa needs these messages to be correctly formatted. Your task is to write a function that takes a string and reverses the characters within each pair of parentheses, removing the parentheses as well.
However, bear in mind that there may be nested parentheses, so you should reverse the characters in the correct order.
const a = decode('hola (odnum)')
console.log(a) // hola mundo
const b = decode('(olleh) (dlrow)!')
console.log(b) // hello world!
const c = decode('sa(u(cla)atn)s')
console.log(c) // santaclaus
// Step by step:
// 1. Reverse the nested -> sa(ualcatn)s
// 2. Reverse the remaining one -> santaclaus
Notes:
The input strings will always be well formed with parentheses that match correctly, you do not need to validate them.
There should not be any parentheses left in the final message.
The maximum nesting level is 2.
My solution AdventJS Challenge day 4
Before explaining the solution, I identified that to solve this problem, I needed a complex regular expression to find patterns of open and closed parentheses.
I'm not good with regular expressions (in fact, I'm quite bad), so for this particular case, I sought help from ChatGPT. Now, I'll proceed to explain the solution.
function decode(message) {
const regex = /\(([^()]+)\)/
let match = message.match(regex)
while (match) {
const reversed = match[1].split('').reverse().join('')
message = message.replace(regex, reversed)
match = message.match(regex)
}
return message
}
Define a regular expression
/\(([^()]+)\)/g
:\(
and\)
match open and closed parentheses, respectively.([^()]+)
is a capture group that matches any character except parentheses, at least once. Theg
modifier indicates global search, meaning it looks for all matches rather than stopping after the first.Initialize the variable
match
usingmessage.match(regex)
. This finds the first match of the regular expression in the message and stores information about the match in the variablematch
.Start a
while
loop that continues as long as there are matches (match !== null
).Inside the loop, you can print the current match to the console with
console.log(match)
(this can be helpful for understanding the process). Inspecting the console withmatch
may return something like this:[ '(cla)', 'cla', index: 4, input: 'sa(u(cla)atn)s', groups: undefined ]
. From these data, we need the value at index 1 to reverse it.Reverse the content of the found substring using
split('')
,reverse()
, andjoin('')
. This is stored in the variablereversed
.Replace the original substring (
message
) with its reversed version in the message usingmessage.replace(regex, reversed)
. This finds the first match and replaces it with the value ofreversed
.Assign a new value to the
match
variable using the updated value stored inmessage
.The loop continues until there are no more matches.
Finally, return the modified message.
Top comments (0)