From 36c65ff54cabd426404fa12f8595dc07c5b6b82f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Marshall?= Date: Thu, 3 Feb 2022 20:48:39 -0300 Subject: [PATCH] exercicio concluido --- semana22/checaParenteses/index.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 semana22/checaParenteses/index.js diff --git a/semana22/checaParenteses/index.js b/semana22/checaParenteses/index.js new file mode 100644 index 0000000..1363ead --- /dev/null +++ b/semana22/checaParenteses/index.js @@ -0,0 +1,26 @@ +function checaParenteses(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; +} \ No newline at end of file