I have function that loops and inside that function I call another function (which should change "newArray[x]" value, but it doesn't do anything).
function UTF8() {
newArray = ["0011", "0001"]
newArray.forEach(x => {
binaryToHexadecimal(newArray[x]);
console.log(newArray[x])
});
console.log(newArray[0])
}
function binaryToHexadecimal(string) {
if (string === "0000") {
return string = "0";
}
else if (string ==="0001") {
return string = "1";
}
else if (string ==="0010") {
return string = "2";
}
else if (string ==="0011") {
return string = "3";
}
else if (string ==="0100") {
return string = "4";
}
}
UTF8();
newArray[x]isundefinedbecausexwill be either0011or0001and bothnewArray["0011"]andnewArray["0001"]don't exist. 2. that's not how you replace a value from an arrayxisn't the index, it's the array element itself 2) you would have to usenewArray[x] = binaryToHexadecimal(newArray[x]);3) this is precisely what.map()is for["0011", "0001"].map(b => parseInt(b, 2).toString(16).toUpperCase());