how to remove HTML tags from a string in JavaScript without using regexp?

I am new to programming and I was solving this exercise. I have tried 3 loops with string.slice() but for some reason it prints an empty string.

Would you please explain what happens inside my code and why it prints the wrong output and how I can correct, rather than giving me your version of the correct answer, so that I can learn from my mistakes.

the test input is

<p><strong><em>PHP Exercises</em></strong></p>

and output should be PHP Exercises

p.s this is not a PHP exercise, I'm not confused

here is my code :

function remove(answer){ 

    var sen = answer.split("");
    var arr = [];
    for (var i = 0; i<answer.length; i++){
        if (answer[i] == "<"){
            for (var j = i; j<answer.length; j++){
                if (answer[j] == ">"){
                    for (var k = j; k<answer.length; k++){
                        if (answer[k] == "<"){
                            return answer.slice(j+1, k);                


Try this:

function stripTags(data)
   var tmpElement = document.createElement("div");
   tmpElement.innerHTML = data;
   return tmpElement.textContent || tmpElement.innerText || "";

var something = '<p><strong><em>PHP Exercises</em></strong></p>';

As to why the provided code isn't working, the code returns when j = 2 and k = 3. I discovered this by writing console.log(j, k); immediately before the return. This insight made it clear that the code is identifying the first set of open tags, when actually you seem to want to identify the open and closed "em" tags. The answers provided by others are more robust, but a quick fix to your code is:


if (answer[i] == "<"){


if (answer.slice(i, i+3) == "<em"){

Hope this helps!

Your code does not account for ... nothing. It simply stops at the first encounter of what's between ">" and "<", which is, in the first case, is nothing! You should check if a character is present, and move on if not.

Honestly, this is one of those useless exercises that text books use to try to get you to think outside the box. But you will never want to loop through a string to find text between tags. There are so many methods built in to JavaScript, it's literally reinventing the wheel to do this... that is if a wheel were really a for-loop.

If you really want to avoid Regex and other built in functions so that you can learn to problem solve the long way, well try slicing by brackets first!

Need Your Help

Multi dimensional array with smarty

php arrays smarty

I am working on a PHP project and I am trying to export a multi dimensional array to Smarty.