Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**** HTML ****/
- <p class="input">}{([](wera))}</p>
- <p class="output"></p>
- /**** JS ****/
- $(document).ready(function() {
- // get souce
- let textInp = $('.input').text();
- //Logic
- let template = new Map();
- template.set('(', ')');
- template.set('{', '}');
- template.set('[', ']');
- let result = stringQuotes(textInp, template);
- //output
- $('.output').text(result);
- });
- function stringQuotes(str, comparativeMap) {
- let stack = [];
- let valuesCompar = comparativeMap.values();
- //each char of string
- for (let i = 0; i < str.length; i++) {
- let charCurr = str.charAt(i);
- //each pair in template map
- for (let entry of comparativeMap) {
- //if char not in pair go to next loop
- if (charCurr != entry[0] && charCurr != entry[1]) {
- continue;
- }
- if (entry[0] == charCurr) { //if quote is open - add it to stack
- stack.push(entry[0]);
- } else if (entry[1] == charCurr && stack[stack.length - 1] == entry[0]){
- stack.pop();
- } else {
- return false;
- }
- }
- }
- return !stack.length;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement