File: //home/evaluation-leave/models/test2.js
function StringChallenge(str) {
const regularOcg = "tkr8ugeay3w604";
const divOcg = "dki8vgay3w604";
const italicOcg = "ik8gay3w604";
const brOcg = "bk8gay3w604";
const prOcg = "pk8gay3w604";
const tagRegex = /<\/?([a-z][a-z0-9]*)\b[^>]*>/g;
let tagStack = [];
let tag;
let matches = [];
while ((tag = tagRegex.exec(str)) !== null) {
matches.push(tag);
}
for (let i = matches.length - 1; i >= 0; i--) {
const tag = matches[i][1];
if (matches[i][0].startsWith("</")) {
tagStack.push(tag);
} else {
if (tagStack.length > 0 && tagStack[tagStack.length - 1] === tag) {
tagStack.pop();
} else {
return tag.startsWith("div")
? divOcg
: tag.startsWith("i")
? italicOcg
: tag.startsWith("p")
? prOcg
: tag.startsWith("b")
? brOcg
: regularOcg;
}
}
}
if (tagStack.length > 0) {
return tag.startsWith("div")
? divOcg
: tag.startsWith("i")
? italicOcg
: tag.startsWith("p")
? prOcg
: tag.startsWith("b")
? brOcg
: regularOcg;
}
return regularOcg;
}
console.log(StringChallenge("<div><b><p>hello world</p></b></div>")); // Output: tkr8ugeay3w604
console.log(StringChallenge("<div><b><p>p</p></b></div>")); // Output: tkr8ugeay3w604
console.log(StringChallenge("<p>hello</p><div></div>")); // Output: tkr8ugeay3w604
console.log(StringChallenge("hello world<p></p><div><em><b></b></em></div>")); // Output: tkr8ugeay3w604
console.log(StringChallenge("<div>")); // Output: dkr8ugeay3w604
console.log(StringChallenge("<div><p></p></div></i>")); // Output: ikr8ugeay3w604
console.log(StringChallenge("<em></em><em></em><p></b>")); // Output: bkr8ugeay3w604
console.log(StringChallenge("<b></p>")); // Output: pkr8ugeay3w604
console.log(StringChallenge("<p><p><em></em><p></p><i></p>")); // Output: ikr8ugeay3w604
console.log(StringChallenge("<i></i><i></i><i></i>abc<i></p>")); // Output: pkr8ugeay3w604