-4

What's supposed to happen is certain profanity typed in and submitted by the user is supposed to be replaced with the censored word.

However, that's not the case. It shows the word without the censor.

Should I use the if/else statements to make this work?

<!DOCTYPE html>
<html>

<head>
  <script>
    function myFunction() {
      var text = document.getElementById('input').value;
      var result = document.getElementById('clean');
      /* Fake 'bad words' for illustration */
      var a = text.replace("ack", "a**");
      var ba = text.replace("bandaid", "b******");
      var bi = text.replace("butch", "b****");
      var f = text.replace("frack", "f****");
      var p = text.replace("pee", "p**");
      var s = text.replace("shoot", "s****");
      var c = text.replace("cart", "c***");
      var n = text.replace("night", "n****");

      result.innerHTML = text;
    }
  </script>
</head>

<body>
  <textarea id="input"></textarea>
  <br />
  <input type="submit" onclick="myFunction();" />
  <p id="clean"></p>
</body>

</html>
double-beep
  • 5,031
  • 17
  • 33
  • 41
Jamie Vic
  • 23
  • 3
  • Replace only works for one instance of the target string. Try `text = text.split('ass').join('a**')` instead. – tewathia Jan 15 '14 at 05:28
  • 4
    [You MUST read this...](http://blog.codinghorror.com/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea/) – ThiefMaster Apr 15 '14 at 08:25

3 Answers3

4

replace returns a new string but does not modify the original.

This means, you should work with the returned value.

In this case, text = text.replace(..)

Edit: Also, you might consider using regular expressions for this: "hello world".replace( /world/g, 'foo' ), so that multiple instances of a search pattern will be replaced.

user2864740
  • 60,010
  • 15
  • 145
  • 220
snwflk
  • 3,341
  • 4
  • 25
  • 37
0

you are assigning the same value in clean which you get from your input try this to show your result in clean applied on the `input'

function myFunction() {
        var text = document.getElementById('input').value;
        var result = document.getElementById('clean');
        text = text.replace("ack","a**");
        text = text.replace("bandaid","b******");
        text = text.replace("butch","b****");
        text = text.replace("frack","f****");
        text = text.replace("pee","p**");
        text = text.replace("shoot","s****");
        text = text.replace("cart","c***");
        text = text.replace("night","n****");

        result.innerHTML=text;
    }
Bart
  • 19,692
  • 7
  • 68
  • 77
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
0

Can you try this, I have rewritten the function for ease in sue.

<script>
    function myFunction() {
        var text = document.getElementById('input').value;
        var result = document.getElementById('clean');
        var Clean =  new Array("ack", "bandaid", "butch", "frack", "pee", "shoot", "cart", "night");
        var len = Clean.length;
        for(var i=0; i<len;i++){  
              text = AddHide(Clean[i], text);
        }
        result.innerHTML=text;
    }

    function AddHide(Text, String){
        if(String!=""){
            var StringLen = parseInt(Text.length)-1;
            var First = Text.charAt(0);             
            for(var k=0; k<StringLen;k++){                
                First +="*";
            }           
            String = String.replace(Text,First);
        }
        return String;
    }

</script>
Bart
  • 19,692
  • 7
  • 68
  • 77
Krish R
  • 22,583
  • 7
  • 50
  • 59