i need help with this coding challenge.
Have the function HTMLElements(str) read the str parameter being passed which will be a string of HTML DOM elements and plain text. The elements that will be used are: b, i, em, div, p. For example: if str is "<div><b><p>hello world</p></b></div>" then this string of DOM elements is nested correctly so your program should return the string true.
If a string is not nested correctly, return the first element encountered where, if changed into a different element, would result in a properly formatted string. If the string is not formatted properly, then it will only be one element that needs to be changed. For example: if str is "<div><i>hello</i>world</b>" then your program should return the string div because if the first element were changed into a <b>, the string would be properly formatted.
example:
Input: "<div><div><b><b/></div><p/>"
output: <div>
Input: "<div>abc</div><p><em><i>test test test</b></em></p>"
output: i
Here is how far I've gotten:
function HTMLElements(str) {
let openingTag = str.match(/<\w+>/g)
let closingTag = str.match(/(<\/\w+>)/g)
let strObj = {
'<div>': '</div>',
'<p>': '</p>',
'<i>': '</i>',
'<p>': '</p>',
'<em>': '</em>',
'<b>': '</b>',
}
let unclosedElem = []
for(let i=0; i<openingTag.length; i++){
console.log(closingTag)
if(closingTag.indexOf(strObj[openingTag[i]]) ===-1){
unclosedElem.push(closingTag.splice(closingTag.indexOf(strObj[openingTag[i]]),1))
}
}
console.log(unclosedElem)
if(unclosedElem.length === 0) return true;
return unclosedElem[0]
}
// keep this function call here
HTMLElements("<div><div><b><b/></div></p>")
now i understand this is far from solving the challenge but i guess it's a start for me. I will eventually solve this but your inputs are appreciated