Functional JS with Tail Call Optimization (TCO)
const test = require('./tester'); const validateTCO = (acc, str) => ( typeof str !== 'string' ? false : str.length === 0 ? acc === 0 : str.charAt(0) === '{' ? validateTCO (acc + 1, str.slice(1)) : str.charAt(0) === '}' ? validateTCO (acc - 1, str.slice(1)) : validateTCO (acc, str.slice(1)) ); const validate = str => validateTCO (0, str); test (validate, [ { in: ['{{}}'], out: true, }, { in: ['{{{'], out: false, }, { in: ['{}{}{}'], out: true, }, { in: ['{abc {def} {{}}}'], out: true, }, ]);
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Functional JS with Tail Call Optimization (TCO)