加密为Unicode的字母不起作用

发布于 2025-01-26 11:22:45 字数 983 浏览 4 评论 0原文

这是从freecodecamp的项目,该项目通过字母的值加密的字符串被13个位置移动 所有字母将是大写。请勿改变任何非字母特征(即空间,标点符号),而要传递。 我测试了REGEX NUMADD的NUMADD等于NUM+13或NUMADD-26。一些信件出错,不要退还大写字母 请有人解释怎么了

function rot13(str) {
    var str2 = '';
    var arr1 = []
    var arr2 = []
    var reg = /[^A-Z]/
    // var char
    if (str.includes(' ') == false) {
        for (let i = 0; i < str.length; i++) {
            var char = '';
            var numres
            let num = str.charCodeAt(i)
            // console.log(num)
            // var numadd = num + 13;
            // if num+13 not uppercase
            if (reg.test(num + 13)) {
                numres = num - 13
            }
            // if num+13 is uppercase
            else {
                numres = num + 13
            }
            char = String.fromCharCode(numres)
            str2 += char
        }
        console.log(str2);
    }
}
rot13("SERRPBQRPNZC");

This is project from freecodecamp where a string is encrypt by the values of the letters are shifted by 13 places
All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
i test numadd by regex numadd is either equal to num+13 or numadd-26. some letter went wrong and do not return an uppercase letter
please someone explain whats wrong

function rot13(str) {
    var str2 = '';
    var arr1 = []
    var arr2 = []
    var reg = /[^A-Z]/
    // var char
    if (str.includes(' ') == false) {
        for (let i = 0; i < str.length; i++) {
            var char = '';
            var numres
            let num = str.charCodeAt(i)
            // console.log(num)
            // var numadd = num + 13;
            // if num+13 not uppercase
            if (reg.test(num + 13)) {
                numres = num - 13
            }
            // if num+13 is uppercase
            else {
                numres = num + 13
            }
            char = String.fromCharCode(numres)
            str2 += char
        }
        console.log(str2);
    }
}
rot13("SERRPBQRPNZC");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2025-02-02 11:22:45

reg.test(num + 13)检查编号的文本表示num + 13(应用 nimit 键入转换)总是说 true (不匹配大写字母)和始终是计算numres = numres = num -13

使用reg.test(String.fromcharcode(num + 13)),如下:

function rot13(str) {
    var str2 = '';
    var arr1 = []
    var arr2 = []
    var reg = /[^A-Z]/
    // var char
    if (str.includes(' ') == false) {
        for (let i = 0; i < str.length; i++) {
            var char = '';
            var numres
            let num = str.charCodeAt(i)
            // console.log(num)
            // var numadd = num + 13;
            // if num+13 not uppercase
            //           ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
            if (reg.test(String.fromCharCode(num + 13))) {
                numres = num - 13
            }
            // if num+13 is uppercase
            else {
                numres = num + 13
            }
            char = String.fromCharCode(numres)
            str2 += char
        }
        console.log(str2);
    }
}
rot13("SERRPBQRPNZC");

reg.test(num + 13) checks the textual representation of the number num + 13 (applying implicit type conversion) so it always says True (do not match an uppercase letter) and always is computed numres = num - 13.

Use reg.test(String.fromCharCode(num + 13)) instead as follows:

function rot13(str) {
    var str2 = '';
    var arr1 = []
    var arr2 = []
    var reg = /[^A-Z]/
    // var char
    if (str.includes(' ') == false) {
        for (let i = 0; i < str.length; i++) {
            var char = '';
            var numres
            let num = str.charCodeAt(i)
            // console.log(num)
            // var numadd = num + 13;
            // if num+13 not uppercase
            //           ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
            if (reg.test(String.fromCharCode(num + 13))) {
                numres = num - 13
            }
            // if num+13 is uppercase
            else {
                numres = num + 13
            }
            char = String.fromCharCode(numres)
            str2 += char
        }
        console.log(str2);
    }
}
rot13("SERRPBQRPNZC");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文