I am trying to replace the 8th character in each element of an array. I know JavaScript does not have a built-in function to replace string characters. I was able to find to potentially helpful code on this site. They accept strings as parameters, but when I pass in a array such as cookies[0], it does not change anything in the string. Why is that? Is there a way I can get this to work? Thanks in advance for any help.
var cookies = ["cartItem1", "cartItem2", "cartItem3", "cartItem4"];
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
function delCookie(){
cookies.splice(0,1);
setCharAt(cookies[0], 8, "Z");
}
delCookie();
I also tried it this way:
var cookies = ["cartItem1", "cartItem2", "cartItem3", "cartItem4"];
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
};
function delCookie2(){
cookies.splice(0,1);
cookies[0].replaceAt(8, "Z");
}
delCookie2();
immutable, which means that they cannot be changed -- ever. They can only be replaced.