Created
February 7, 2022 14:07
-
-
Save harryWonder/f99e0bc0adef15137e452f4f112bf426 to your computer and use it in GitHub Desktop.
MetaCare Algorithm Test
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
/** | |
* | |
1. Build a function to accept the string as an arguement. | |
2. Capture the length of the string. | |
3. Determine that the string length is an even number. This would provide certainty that the brackets are even. | |
4. Write a loop based on the length of the string but divided. | |
5. Inside the loop, take the current index of the string and the inverse index of the string based on the loop index. | |
6. Concat the string and look for a match. | |
7. Push the results into an array and filter out negative values. | |
**/ | |
function matchAlg(data) { | |
let resultSet = []; | |
let strLength = data.length; | |
let validators = [ | |
"{", | |
"(", | |
"[", | |
"}", | |
"]", | |
")", | |
]; /* This can serve as validators */ | |
/* Validate The First Half */ | |
if (strLength % 2 == 0) { | |
/* Write A Loop Based On The Index Of The String */ | |
for (let i = 0; i < (strLength / 2); i++) { | |
/* Validate That Only The Brackets Are In The Array */ | |
if (validators.includes(data[i])) { | |
let matchFirstIndex = data[i]; //{} | |
let matchInverseIndex = data[data.length - (i + 1)]; // | |
/* Concat The Indexes */ | |
let concatOp = `${matchFirstIndex}${matchInverseIndex}`; | |
console.log(concatOp); | |
if (concatOp == '{}' || concatOp == '[]' || concatOp == '()' || concatOp == ')(' || concatOp == '}{' || concatOp == '][') { | |
resultSet.push(true); | |
} else { | |
resultSet.push(false); | |
} | |
} | |
} | |
/* Validate The Result Set Only Has Truth Values */ | |
return !(resultSet.includes(false)) ? 'Balanced' : 'UnBalanced'; | |
} | |
return 'UnBalanced'; | |
} | |
console.log(matchAlg("{{}}")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment