Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. /**** HTML ****/
  2. <p class="input">}{([](wera))}</p>
  3. <p class="output"></p>
  4.  
  5. /**** JS ****/
  6. $(document).ready(function() {
  7. // get souce
  8. let textInp = $('.input').text();
  9.  
  10. //Logic
  11. let template = new Map();
  12. template.set('(', ')');
  13. template.set('{', '}');
  14. template.set('[', ']');
  15. let result = stringQuotes(textInp, template);
  16. //output
  17. $('.output').text(result);
  18. });
  19.  
  20. function stringQuotes(str, comparativeMap) {
  21. let stack = [];
  22. let valuesCompar = comparativeMap.values();
  23. //each char of string
  24. for (let i = 0; i < str.length; i++) {
  25. let charCurr = str.charAt(i);
  26. //each pair in template map
  27. for (let entry of comparativeMap) {
  28. //if char not in pair go to next loop
  29. if (charCurr != entry[0] && charCurr != entry[1]) {
  30. continue;
  31. }
  32. if (entry[0] == charCurr) { //if quote is open - add it to stack
  33. stack.push(entry[0]);
  34. } else if (entry[1] == charCurr && stack[stack.length - 1] == entry[0]){
  35. stack.pop();
  36. } else {
  37. return false;
  38. }
  39. }
  40. }
  41. return !stack.length;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement