gpt4 book ai didi

javascript - 如何在 String.fromCharCode() 中使用变量?

转载 作者:行者123 更新时间:2023-12-01 02:42:49 25 4
gpt4 key购买 nike

我正在尝试使用下面的代码将 unicode 转换为字符串,但没有成功,请帮忙?

function rot13(str) {
var message = "";
for (var i = 0; i < str.length; i++) {
message += str.charCodeAt(i) + " ";
}
message = message.split(" ").filter(Boolean).join(",");
return String.fromCharCode(message);
}
console.log(rot13("Hello World"))

最佳答案

fromCharCode("1,1,1,1")fromCharCode(1,1,1,1) 不同,您需要使用 apply用一个数组。

function rot13(str) {
var message = "";
for (var i = 0; i < str.length; i++) {
message += str.charCodeAt(i) + " ";
}
message = message.split(" ").filter(Boolean);
return String.fromCharCode.apply(String, message);
}
console.log(rot13("Hello World"))

关于javascript - 如何在 String.fromCharCode() 中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47441795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com