AlgoDaily Solution

1var assert = require('assert');
2
3function validateSymbols(str) {
4 let map = {
5 ')': '(',
6 ']': '[',
7 '}': '{',
8 };
9 let stack = [];
10 for (let i = 0; i < str.length; i++) {
11 if (str[i] === '(' || str[i] === '[' || str[i] === '{') {
12 stack.push(str[i]);
13 } else {
14 if (stack[stack.length - 1] === map[str[i]]) {
15 stack.pop();
16 } else return false;
17 }
18 }
19 return stack.length === 0 ? true : false;
20}
21
22try {
23 assertIsFunction(validateSymbols);
24
25 console.log('PASSED: ' + '`validateSymbols` is a function');
26} catch (err) {
27 console.log(err);
28}
29
30try {
31 assert.equal(validateSymbols('[]'), true);
32
33 console.log('PASSED: ' + "`validateSymbols('[]')` should return `true`");
34} catch (err) {
35 console.log(err);
36}
37
38try {
39 assert.equal(validateSymbols('{{[]}}'), true);
40
41 console.log('PASSED: ' + "`validateSymbols('{{[]}}')` should return `true`");
42} catch (err) {
43 console.log(err);
44}
45
46try {
47 assert.equal(validateSymbols('[[}}'), false);
48
49 console.log('PASSED: ' + "`validateSymbols('[[}}')` should return `false`");
50} catch (err) {
51 console.log(err);
52}
53
54function assertIsFunction(f) {
55 return typeof f === 'function';
56}
Community Solutions
Community solutions are only available for premium users.
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.
xxxxxxxxxx
43
var assert = require('assert');
function validateSymbols(str) {
// implement this method
return str;
}
try {
assertIsFunction(validateSymbols);
console.log('PASSED: ' + '`validateSymbols` is a function');
} catch (err) {
console.log(err);
}
try {
assert.equal(validateSymbols('[]'), true);
console.log('PASSED: ' + "`validateSymbols('[]')` should return `true`");
} catch (err) {
console.log(err);
}
try {
assert.equal(validateSymbols('{{[]}}'), true);
console.log('PASSED: ' + "`validateSymbols('{{[]}}')` should return `true`");
OUTPUT
Results will appear here.