Skip to content

Instantly share code, notes, and snippets.

@ErickCodigo
Created November 1, 2020 18:30
Show Gist options
  • Save ErickCodigo/e84f3bb818455752d4a5fb61fa576ec0 to your computer and use it in GitHub Desktop.
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
/*
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