I have written my code
JavaScript
x
8
1
function takesst(str) {
2
const str1 = [str];
3
const l = str1.length;
4
const check = (str1) => (str1.indexOf('(') === 0 && str1.lastIndexOf(')') === l - 1);
5
res = check(str1);
6
return res;
7
}
8
The goal is to check that the first charachter is (
and the last )
. Works OK.
JavaScript
1
3
1
node mycode.js
2
true
3
Now, the next step would be the high-order function that would include removing first and last character at each step and checking if the condition is met again.
JavaScript
1
2
1
const rec = f => f((str).slice(1,-1))
2
String befor and after this step.
JavaScript
1
3
1
'(()())'
2
'()()'
3
How to do this?
Advertisement
Answer
You could check for length and if the string starts and ends with wanted characters and the rest fullfills the same objective.
JavaScript
1
4
1
const check = string => !string.length || string.startsWith('(') && string.endsWith(')') && check(string.slice(1, -1));
2
3
console.log(check('((()))'));
4
console.log(check('((())'));