Created
November 1, 2020 18:30
-
-
Save ErickCodigo/e84f3bb818455752d4a5fb61fa576ec0 to your computer and use it in GitHub Desktop.
Resolviendo un ejercicio de BettaTech - https://www.youtube.com/watch?v=Sc-rjCODBDY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Dado un string formado por (), [], {} escribe un programa que indique | |
si los pares correspondientes son correctos. | |
Entrada: [()] {} {[()()]()} Correcto | |
Entrada: [(]) Incorrecto | |
*/ | |
function checkEvenPairs(stringToEvaluate) { | |
let state = true; | |
let currentIndex = -1; | |
const positionsRecorder = []; | |
const openSigns = ["[", "(", "{"]; | |
const closeSigns = ["]", ")", "}"]; | |
const arrayToEvaluate = stringToEvaluate.replaceAll(" ", "").split(""); | |
while (state && currentIndex < arrayToEvaluate.length - 1) { | |
currentIndex++; | |
const currentSign = arrayToEvaluate[currentIndex]; | |
if (openSigns.includes(currentSign)) | |
positionsRecorder.push(openSigns.indexOf(currentSign)); | |
else { | |
let lastIndexOpenSign = positionsRecorder[positionsRecorder.length - 1]; | |
const closeIndex = closeSigns.indexOf(currentSign); | |
if (lastIndexOpenSign === closeIndex) positionsRecorder.pop(); | |
else state = false; | |
} | |
} | |
return state; | |
} | |
console.log(checkPairs("[(])")); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment