0

我写了一个简单的 for 循环,以更好地理解 charCodeAt() 和 fromCharCode() 并解决 odin 项目的 Caesar 密码练习,您必须编写函数来编码字符串。

它在第一次迭代时停止,我不知道为什么。当我控制台记录 str.charCodeAt(i) 时,它会循环,但是当我添加函数的其余部分时,它会停止。

function uniCode(str, num) {
    for(let i = 0; i < str.length;i++) {
        //console.log(str.charCodeAt(i) + num);
        let charCode = str.charCodeAt(i) + num;
        let newStr = String.fromCharCode(charCode);
        return newStr;
    }        
        
}
uniCode("Help!", 3);
'K'

很高兴有任何帮助!

谢谢!

4

3 回答 3

1

这里的问题是return导致问题的循环末尾的语句。

您可以创建一个变量并附newStr加到它并在循环之后返回字符串值

function uniCode(str, num) {
    let finalStr = ""
    for(let i = 0; i < str.length;i++) {
        let charCode = str.charCodeAt(i) + num;
        let newStr = String.fromCharCode(charCode);
        finalStr = finalStr + newStr;
    }        
 return finalStr       
}

uniCode("Help!", 3);
'Khos$'

您可以使用以下reduce功能使代码更好:

function uniCode(str, num) {
  return str
    .split('')
    .map(x=>x.charCodeAt(0))
    .reduce((total, curr) => (total + String.fromCharCode(curr + num)), "")
}
于 2022-01-04T10:48:11.013 回答
1

使用您的代码,您可以遍历参数中给定字符串的所有字符

但是你return newStr;在循环中它会停止循环迭代

function uniCode(str, num) {
    var newStr = "";
    for(let i = 0; i < str.length;i++) {
        console.log(str.charCodeAt(i) + num);
        let charCode = str.charCodeAt(i);
        newStr += String.fromCharCode(charCode);
    }        
    console.log(newStr);
    return newStr;
        
}
uniCode("Help!", 3);

于 2022-01-04T10:43:36.577 回答
1

它正在停止,因为您已经通过 using 告诉它return newStr;,它会立即终止函数并返回newStr

对于你正在做的事情,你想建立新的字符串,然后在最后返回它:

function uniCode(str, num) {
    let newStr = "";

    for (let i = 0; i < str.length; i++) {
        let charCode = str.charCodeAt(i) + num;
        let newChar = String.fromCharCode(charCode);
        newStr += newChar;
    }        
        
    return newStr;
}

现场示例:

function uniCode(str, num) {
    let newStr = "";

    for (let i = 0; i < str.length; i++) {
        let charCode = str.charCodeAt(i) + num;
        let newChar = String.fromCharCode(charCode);
        newStr += newChar;
    }        
        
    return newStr;
}

console.log(uniCode("Help!", 3)); // "Khos$"

但是请注意,当您遍历这样的字符串时,您正在使用代码单元,而不是代码点,如果存在需要多个代码单元来处理的代码点,这可能会使您的结果非常奇怪。(如果这些术语不熟悉,请参阅我的博客文章。)您可能想for-of改用它,因为它适用于代码点:

function uniCode(str, num) {
    let newStr = "";

    for (const char of str) {
        const codePointValue = char.codePointAt(0) + num;
        const newChar = String.fromCodePoint(codePointValue);
        newStr += newChar;
    }        
        
    return newStr;
}

现场示例:

function uniCode(str, num) {
    let newStr = "";

    for (const char of str) {
        const codePointValue = char.codePointAt(0) + num;
        const newChar = String.fromCodePoint(codePointValue);
        newStr += newChar;
    }        
        
    return newStr;
}

function oldUniCode(str, num) {
    let newStr = "";

    for (let i = 0; i < str.length; i++) {
        let charCode = str.charCodeAt(i) + num;
        let newChar = String.fromCharCode(charCode);
        newStr += newChar;
    }        
        
    return newStr;
}

console.log("New: " + uniCode("Help!", 3));    // "New: Khos$"
console.log("Old: " + oldUniCode("Help!", 3)); // "Old: Khos$"

于 2022-01-04T10:45:34.853 回答