【问题标题】:For loop is stopping when rest of the block is added当添加块的其余部分时,for循环正在停止
【发布时间】:2022-01-04 10:41:19
【问题描述】:

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

它在第一次迭代时停止,我不知道为什么。 当我控制台记录 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'

很高兴得到任何帮助!

谢谢!

【问题讨论】:

    标签: javascript loops for-loop fromcharcode


    【解决方案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);

    【讨论】:

    • newStr 放在函数之外是不好的做法。相反,在函数中构建字符串并返回它。
    • 是的,谢谢你,我用你的评论修复了我的样本
    • 谢谢!我完全忘记了返回停止函数
    【解决方案2】:

    它停止是因为你告诉它,使用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$"

    请注意,不过,当您循环访问这样的字符串时,您是在 代码单元 中工作,而不是在 代码点 中工作,如果存在需要多个代码单元来处理的代码点,这可能会使您的结果非常奇怪。 (如果不熟悉这些术语,请参阅 my blog post here。)您可能想改用 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$?"

    【讨论】:

    • 非常感谢!我会读你的博文!
    【解决方案3】:

    这里的问题是导致问题的循环末尾的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)), "")
    }
    

    【讨论】:

    • 谢谢!没有考虑使用reduce()。如果你知道如何处理它,Reduce() 就会非常灵活。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多