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
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
Share Improve this question edited Jun 19, 2020 at 7:56 cris asked Jun 19, 2020 at 2:28 criscris 2251 gold badge6 silver badges12 bronze badges 5- Your question is likely to get downvoted if you don't prove you've made any effort. But you just copy/pasted the challenge, and expect people to solve it for you. Becoming a better developer requires trying to solve problems, and eventually getting help with your attempt if you get stuck somewhere. Giving you a solution would just be cheating, not learning, don't you think? – blex Commented Jun 19, 2020 at 2:36
- 1 thanks. @blex. You're right. will work on this myself – cris Commented Jun 19, 2020 at 2:39
- Come back with your attempt, and we'll gladly help – blex Commented Jun 19, 2020 at 2:41
- @blex You're right. I think he is doing this challenge for Vinove software company.