At the exit, I get those elements that were originally. That is, the assignment does not work.

function deleteOtherChars(arr) { for (var i = 0; i < arr.length; i++) { for (var j = 0; j < arr[i].length; j++) { arr[i][j] = 'd'; console.log(arr[i][j]); } } //console.log(arr); } var arr = ['aaaaa','bbbbb', 'ccccc']; deleteOtherChars(arr); 

  • specify what should be the output - sscream
  • I need to replace the nth element of the string, in the array - zh-mskl9
  • You are trying to make an assignment to a character in a string, and not to an array element. JS doesn't work that way. In JS, a string is not an array of characters. - Alexey Ten

2 answers 2

In js, it will not be possible to replace the character of a string by referring to it as an element of an array. String characters can be changed like this.

 var string = 'test', result = string.replace(string[1], 'o'); console.log (result) // tost 

UPDATE

the method above is not correct, with the same symbols the first entry will be replaced, you can do so

 function replaceChar (string, index, char){ return string.slice(0,index)+char+string.slice(index+1) } 
  • Thank you, tormented for half a day)) - zh-mskl9
  • one
    result = string.replace(string[3], 'o'); - what do you think will work out? - Alexey Ten

Another option is to use a regular expression when replacing:

 function replaceCharByIndex(string, index, char) { var regExp = new RegExp('(.{' + index + '})(.)'); return string.replace(regExp, '$1' + char); } console.log(replaceCharByIndex('test',1,'o')); console.log(replaceCharByIndex('test',3,'o'));