const string = "([{{}}])";
const patternMatch = {
")": "(",
"]": "[",
"}": "{",
};
const validates = (input) => {
/**
* Spread the text to iterate over it
* Input = ([]) , Output = ["(","[","]",")"]
*/
const array = [...input];
/**
* Get the elements from end
*/
let endIndex = array.length - 1;
let result = true;
/**
* Look closely at patternMatch above,
* if we don't find left hand side anymore(keys)
* then we have reached the other half(values)
*/
array.forEach((item, index) => {
if (!patternMatch[array[endIndex - index]]) {
return;
}
/**
* All this does is get the element
* from the last by using index
* Otherwise we would have to use pointer
* which I'm not a fan of
*/
if (item === patternMatch[array[endIndex - index]]) {
result = true;
} else {
result = false;
return;
}
});
return result;
};
console.log(validates(string));