0

Now I am about to share a small code, which is not working and I don't have any idea why the heck it is not working:

HTML:

<!DOCTYPE html>
<html lang="en-US">
    <head>
        <title>Stack Overflow</title>
        <meta charset='UTF-8'>
        <link rel="stylesheet" type="text/css" href="css.css">
    </head>
    <body>
        <div id="targetField">Please Enable Javascript</div>
        <script type="text/javascript" src="javascript.js"></script>
    </body>
</html>

JS:

function changeString(stringA , a , b){
    var character = stringA.charAt(a);
    stringA.charAt(a) = stringA.charAt(b);
    stringA.charAt(b) = character;
    return stringA;
}

var stringB = changeString("This is javascript" , 1 , 2 );
document.getElementById('targetField').textContent = stringB;

This looks very wierd. Can someone please tell me the problem. (I hope there is no syntax error)

codetalker
  • 576
  • 1
  • 6
  • 21

1 Answers1

0

You will find an answer here.

.chartAt(index) is a function and returns the character at the specified index. It does not let you set a character!

If you want to change the string you have to create a new one.

Community
  • 1
  • 1
b-pos465
  • 131
  • 4