I have been able to successfully convert my characters to Unicode, add 1, however I am having trouble with the final step of converting back to characters. I am not sure why the last statement in the if statement is not working. Please help!
function LetterChanges(str) { for (var i = 0; i < str.length; i++) { var c = str.charCodeAt(i); if (c > 64 && c < 127) { str = str.replace(str.charAt(i), c + 1); str = str.replace(str.charAt(i), str.fromCharCode(i)); } } console.log(str); } LetterChanges("hello*3");
fromCharCode
is meant to be called on the String
object itself as a static method that you pass your instance into.
Additionally, charAt(i)
is functionally equivalent to [i]
.
While I'm not exactly sure what you are after here, you can see the implementation of it here:
function LetterChanges(str) { for(var i = 0; i < str.length; i++){ var c = str.charCodeAt(i); if(c > 64 && c < 127){ str = str.replace(str[i], String.fromCharCode(c + 1)); } } console.log(str); } LetterChanges("hello*3");
You are calling fromCharCode(i)
, but i
is the index into the string, not the character code. You want to pass the character code: String.fromCharCode(c+1)
or whatever.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.