Rules: You should create a function that receives a string with brackets, parentheses, or curly brackets, an example:
test('{A}')
The function must check that the opening and closing of the brackets are correct. If they are correct it should return true
, else it should return false
.
Examples:
'{A}' // true
'[A]' // true
'(AB)' // true
'({A})' // true
'((A))' // true
'[(A)]' // true
'[A}' // false
'(A]' // false
'({A])' // false
'([A))' // false
This is the code I did to resolve this problem:
const bracketList = ['{', '[', '('];
// This function return the reverse bracket. For example '[' returns ']';
const getReverseBracket = (bracket) => {
if (bracket == '{') return '}';
if (bracket == '[') return ']';
if (bracket == '(') return ')';
}
// This function return the initial bracket. For example if the string starts with '{', then it returns '{'.
// In case the string does not start with any bracket, then return null
const starterBracket = (string) => {
for (brak of bracketList) {
if (string[0] == brak) return brak
}
return null
}
// This function is called when any starter bracket has been detected.
// if the string start with '(', then it checks if the reverse bracket is in the end of the string.
// If this is true, then returns true, else returns false
const bracketOk = (string, bracket) => {
let reverseBracket = getReverseBracket(bracket);
if (string.slice(-1) == reverseBracket) return true
else return false;
}
// This function removes the brackets of the strings
const removeBrackets = (string, bracket) => {
let reverseBracket = getReverseBracket(bracket);
return string.replace(bracket, '').replace(reverseBracket, '');
}
// This function checks if the string contains any reverse or normal bracket. Depending of the result it return a number that means:
// ! 0: if the string contains reverse bracket but no Starter
// * 1: if the string contain starter bracket
// ? 2: if the string does not contain any bracket
const containAnyBracket = (string) => {
const rbracketList = ['}', ']', ')'];
for (brak of bracketList) {
if (string.includes(brak)) return 1
}
for (rbrak of rbracketList) {
if (string.includes(rbrak)) return 0
}
return 2;
}
// this function calls all other functions to find out if the string meets the requirements.
function testString(string) {
let bracket = starterBracket(string)
if (bracket) {
if (bracketOk(string, bracket)) {
let noBracketString = removeBrackets(string, bracket);
let containBracket = containAnyBracket(noBracketString);
if (containBracket == 2) return true;
else if (containBracket == 1) {
bracket = starterBracket(noBracketString);
if (bracketOk(noBracketString, bracket)) return true;
else return false;
}
else if (containBracket == 0) return false;
} else return false;
} else return false;
}
How would you improve the solution code?. I honestly think i could do it better. How you would solve this?
Do you have any advice for me about good practices?
getReverseBracket
with a plain object that holds the opening bracket as the key, and the closing as the value, like this:const reverseBracket = {'(': ')'}
(of course, you'd need to add all the missing mappings) \$\endgroup\$for (name of list) {...}
, you're creating a global variable calledname
(name
is just a placeholder for your multiple times of doing this). Usefor (const name of list) {...}
. Another thing is you're inconsistent with your use of semicolons. In yourbracketOk
andcontainAnyBracket
functions, you don't use;
afterreturn
statements, but you do everywhere else. In yourgetReverseBracket
function, I would either make itelse if
for the second and thirdif
, or make it aswitch
statement. \$\endgroup\$