Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions logic-exercises/exercise-day-3/checkParenthesis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function checkParenthesis(str) {
var stack = [];
for (var _i = 0, str_1 = str; _i < str_1.length; _i++) {
var char = str_1[_i];
if (char === "(" || char === "[" || char === "{") {
stack.push(char);
}
else {
var lastOpeningChar = stack.pop();
if (!lastOpeningChar) {
return false;
}
else if ((lastOpeningChar === "(" && char !== ")") ||
(lastOpeningChar === "[" && char !== "]") ||
(lastOpeningChar === "{" && char !== "}")) {
return false;
}
}
}
if (stack.length > 0) {
return false;
}
return true;
}
console.log(checkParenthesis("()"));
console.log(checkParenthesis("(]"));
console.log(checkParenthesis("({})"));
console.log(checkParenthesis("([})"));
Empty file.
28 changes: 28 additions & 0 deletions logic-exercises/exercise-day-3/checkParenthesis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function checkParenthesis(str) {
const stack = []
for (let char of str) {
if (char === "(" || char === "[" || char === "{") {
stack.push(char)
} else {
const lastOpeningChar = stack.pop()
if(!lastOpeningChar) {
return false
} else if (
(lastOpeningChar === "(" && char !== ")") ||
(lastOpeningChar === "[" && char !== "]") ||
(lastOpeningChar === "{" && char !== "}")
) {
return false
}
}
}
if (stack.length > 0) {
return false
}
return true
}

console.log(checkParenthesis("()"))
console.log(checkParenthesis("(]"))
console.log(checkParenthesis("({})"))
console.log(checkParenthesis("([})"))
13 changes: 13 additions & 0 deletions logic-exercises/exercise-day-3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "exercise-day-3",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start":"clear && tsc --outFile checkParenthesis.js checkParenthesis.ts && node checkParenthesis.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}