Hello
include me
World
don't include me
{"ScriptPreparationCode":null,"TestCases":[{"Name":"Better Solution","Code":"function getPath(a) {\r\n var path = [];\r\n while (a !== null) {\r\n path.push(a);\r\n a = a.parentNode;\r\n }\r\n path.reverse();\r\n return path;\r\n}\r\n\r\nfunction getLCA(a,b) {\r\n var aPath = getPath(a);\r\n var bPath = getPath(b);\r\n var lca;\r\n for (var i = 0; i \u003C aPath.length \u0026\u0026 aPath[i] === bPath[i]; i\u002B\u002B) {\r\n lca = aPath[i];\r\n }\r\n return lca;\r\n}\r\n\r\nfunction getHighlightedText(a, b) {\r\n var passedA = false;\r\n var passedB = false;\r\n var text = \u0027\u0027;\r\n var lca = getLCA(a,b);\r\n\r\n function traverse(node) {\r\n if (node === a) {\r\n passedA = true;\r\n }\r\n if (passedA \u0026\u0026 node.nodeType === 3) {\r\n text \u002B= node.nodeValue;\r\n }\r\n if (node === b) {\r\n passedB = true;\r\n return;\r\n }\r\n var child = node.firstChild;\r\n while (child !== null \u0026\u0026 !passedB) {\r\n traverse(child);\r\n child = child.nextSibling;\r\n }\r\n }\r\n traverse(lca);\r\n\r\n return text;\r\n}\r\n\r\ngetHighlightedText(\r\n document.getElementById(\u0027start\u0027).firstChild,\r\n document.getElementById(\u0027end\u0027).firstChild\r\n);","IsDeferred":false},{"Name":"Optimal Solution","Code":"function getHighlightedText(a, b) {\r\n var text = a.nodeValue;\r\n var cur = a;\r\n while (cur !== b) {\r\n if (cur.firstChild !== null) {\r\n \tcur = cur.firstChild;\r\n } else {\r\n \twhile (cur.nextSibling === null) {\r\n \tcur = cur.parentNode;\r\n }\r\n cur = cur.nextSibling;\r\n\t}\r\n if (cur.nodeType === 3) {\r\n\t text \u002B= cur.nodeValue;\r\n }\r\n }\r\n return text;\r\n}\r\n\r\ngetHighlightedText(\r\n document.getElementById(\u0027start\u0027).firstChild,\r\n document.getElementById(\u0027end\u0027).firstChild\r\n);","IsDeferred":false},{"Name":"TreeWalker","Code":"function getHighlightedText(a, b) {\r\n var passedA = false; \r\n var text = \u0027\u0027; \r\n \r\n const treeWalker = document.createTreeWalker(\r\n document.body,\r\n NodeFilter.SHOW_TEXT\r\n );\r\n\r\n while (treeWalker.nextNode()) {\r\n const node = treeWalker.currentNode;\r\n \tif (node === a) {\r\n passedA = true;\r\n }\r\n if (passedA) {\r\n \ttext \u002B= node.nodeValue;\r\n }\r\n if (node === b) {\r\n break;\r\n }\r\n }\r\n\r\n return text;\r\n}\r\n\r\ngetHighlightedText(\r\n document.getElementById(\u0027start\u0027).firstChild,\r\n document.getElementById(\u0027end\u0027).firstChild\r\n);","IsDeferred":false},{"Name":"TreeWalker with LCA","Code":"function getPath(a) {\r\n var path = [];\r\n while (a !== null) {\r\n path.push(a);\r\n a = a.parentNode;\r\n }\r\n path.reverse();\r\n return path;\r\n}\r\n\r\nfunction getLCA(a,b) {\r\n var aPath = getPath(a);\r\n var bPath = getPath(b);\r\n var lca;\r\n for (var i = 0; i \u003C aPath.length \u0026\u0026 aPath[i] === bPath[i]; i\u002B\u002B) {\r\n lca = aPath[i];\r\n }\r\n return lca;\r\n}\r\n\r\nfunction getHighlightedText(a, b) {\r\n var passedA = false; \r\n var text = \u0027\u0027;\r\n var lca = getLCA(a,b);\r\n \r\n const treeWalker = document.createTreeWalker(\r\n lca,\r\n NodeFilter.SHOW_TEXT\r\n );\r\n\r\n while (treeWalker.nextNode()) {\r\n const node = treeWalker.currentNode;\r\n \tif (node === a) {\r\n passedA = true;\r\n }\r\n if (passedA) {\r\n \ttext \u002B= node.nodeValue;\r\n }\r\n if (node === b) {\r\n break;\r\n }\r\n }\r\n\r\n return text;\r\n}\r\n\r\ngetHighlightedText(\r\n document.getElementById(\u0027start\u0027).firstChild,\r\n document.getElementById(\u0027end\u0027).firstChild\r\n);","IsDeferred":false},{"Name":"Creative Solution","Code":"function getHighlightedText(a, b) {\r\n var all = document.getElementsByTagName(\u0027*\u0027);\r\n var filtered = \u0022\u0022;\r\n var aPassed = false;\r\n for (var i = 0; i \u003C all.length; i\u002B\u002B) {\r\n var elem = all[i];\r\n if (elem.firstChild === a) {\r\n aPassed = true;\r\n }\r\n if (elem.firstChild \u0026\u0026 elem.firstChild.nodeType === 3 \u0026\u0026 aPassed) {\r\n filtered \u002B= elem.firstChild.nodeValue;\r\n }\r\n if (elem.firstChild == b) {\r\n break;\r\n }\r\n };\r\n return filtered;\r\n}\r\n\r\ngetHighlightedText(\r\n document.getElementById(\u0027start\u0027).firstChild,\r\n document.getElementById(\u0027end\u0027).firstChild\r\n);","IsDeferred":false}]}