Issue
I got this warning from variable cipher
when I was trying to change a string character at specified index.
const emojis: string[] = [/* values */];
function revealOriginEmojis(cipher: string): string {
for(let i = 0; i < cipher.length; i++){
let index: number = emojis.indexOf(cipher[i]);
cipher[i] = emojis[index];
}
return cipher;
}
So, should I create a new string variable or any better solutions? Thank you so much
Solution
A string
is a primitive value, it's immutable.
you can convert it into array of chars, edit some elements of the array and convert back the array into string.
const cipherChars = [...cipher]; // convert into array
cipherChars[2] = 'X'; // alter array
cipher = cipherChars.join(''); // convert back into string
Answered By - Yukulélé
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.