curriculum/challenges/english/blocks/daily-coding-challenges-javascript/6821ebda237de8297eaee792.md
Given a string that contains properly nested parentheses, return the decoded version of the string using the following rules:
decode("(f(b(dc)e)a)") should return "abcdef".
assert.equal(decode("(f(b(dc)e)a)"), "abcdef");
decode("((is?)(a(t d)h)e(n y( uo)r)aC)") should return "Can you read this?".
assert.equal(decode("((is?)(a(t d)h)e(n y( uo)r)aC)"), "Can you read this?");
decode("f(Ce(re))o((e(aC)m)d)p") should return "freeCodeCamp".
assert.equal(decode("f(Ce(re))o((e(aC)m)d)p"), "freeCodeCamp");
function decode(s) {
return s;
}
function decode(s) {
while (s.includes(')')) {
const closeIndex = s.indexOf(')');
const openIndex = s.lastIndexOf('(', closeIndex);
const before = s.slice(0, openIndex);
const group = s.slice(openIndex + 1, closeIndex).split('').reverse().join('');
const after = s.slice(closeIndex + 1);
s = before + group + after;
}
return s;
}