It is just can be solved by checking if there are parenthesis, they are closed (so to test there aren´t only one “(” or only one ") and not it´s correspondent too )
For this I made this which works but it´s a little verbose, right:
let leftpar = /\)/
let rightpar = /\(/
// check if there are missing closing parenthesis
if (str.match(leftpar) || str.match(rightpar)){
if (str.match(leftpar)){
if (str.match(rightpar)){
1==1 //do nothing
}
else{
return false
}
}
else if (str.match(rightpar)){
if (str.match(leftpar)){
1==1 // do nothing
}
else{
return false
}
}
}
I was wondering there could be an approach less verbose.(but I don´t want to look at solutions since I have left another test to pass which forces me to change somethings in my main code)
I could remember the time I was going through this challenge. Solved it with help from regular expressions. My suggestion is to spend a little time learning nuts and bolts of regular expressions. It’ll help you in the long run. As a resource, there is plenty of online regex previewers. Have fun
Another way to accomplish the same thing above is:
if (!str.match(rightpar)){
return false
}
Also, if you just want to validate all parentheses are closed, you could do:
let leftpar = /\)/
let rightpar = /\(/
const numLeftpar = (str.match(leftpar) || []).length;
const numRightpar = (str.match(rightpar) || []).length;
if (numLeftpar !== numRightpar) {
return false;
}
However, since the real point of this challenge is to use regular expressions, you should probably figure out how to solve this with a single regular expression (which is possible).