为了账号安全,请及时绑定邮箱和手机立即绑定

Javascript“声明但从未使用过的变量”......即使我正在使用它?

Javascript“声明但从未使用过的变量”......即使我正在使用它?

ITMISS 2022-10-13 16:01:10
我肯定错过了什么。在下面的代码中,我清楚地声明loopingAdjustment,然后在它下面我在fromCharCode函数中调用它。所以,我正在使用它,对吧?我应该可以调用它,因为它在同一个范围内,对吧?为什么 VSCode 说它“从未使用过”,为什么我的终端说它“未定义”?谢谢你。const caesar = function(startingString, shiftAmount) {        let itemizedString = startingString.split('');    const mappedLetters = itemizedString.map(stringLetter => {                //turn each letter in array into their respective character code        let thisLetter = stringLetter.charCodeAt(stringLetter);        // checking if character is alphabetic and converting its charcode back to a letter        if (thisLetter < 65 || (thisLetter > 90 && thisLetter < 97) || thisLetter > 122) {            return;        } else {            shiftedLetter = thisLetter + shiftAmount;        }                // making sure the shifted letters loop to beginning, or end, of alphabet        if (thisLetter > 96 && shiftedLetter > 122) {            let loopingAdjustment = shiftedLetter - 26;        } else if (thisLetter > 96 && shiftedLetter < 96) {            let loopingAdjustment = shiftedLetter + 26;        } else if (thisLetter < 91 && shiftedLetter > 90) {            let loopingAdjustment = shiftedLetter - 26;        } else if (thisLetter < 91 && shiftedLetter < 65) {            let loopingAdjustment = shiftedLetter + 26;        } else {            let loopingAdjustment = shiftedLetter;        }        let finalString = String.fromCharCode(loopingAdjustment);        return finalString;    });    console.log(mappedLetters);    return mappedLetters.join('');}module.exports = caesar
查看完整描述

2 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

这是因为范围而发生的,因为您仅在 if 语句中声明和定义了 loopingAdjustment 变量,因此范围仅限于 if 语句。要解决此问题,您可以在 if 之外声明 loopingAdjustment 并在 if 语句中分配它。您可以从下面的代码中获取参考,您可以修改 acc. 根据您的需要。


const caesar = function(startingString, shiftAmount) {

    

    let itemizedString = startingString.split('');


    const mappedLetters = itemizedString.map(stringLetter => {

        

        //turn each letter in array into their respective character code

        let thisLetter = stringLetter.charCodeAt(stringLetter);


        // checking if character is alphabetic and converting its charcode back to a letter

        if (thisLetter < 65 || (thisLetter > 90 && thisLetter < 97) || thisLetter > 122) {

            return;

        } else {

            shiftedLetter = thisLetter + shiftAmount;

        }

        

        // making sure the shifted letters loop to beginning, or end, of alphabet

        let loopingAdjustment = 0;

        if (thisLetter > 96 && shiftedLetter > 122) {

            loopingAdjustment = shiftedLetter - 26;

        } else if (thisLetter > 96 && shiftedLetter < 96) {

            loopingAdjustment = shiftedLetter + 26;

        } else if (thisLetter < 91 && shiftedLetter > 90) {

            loopingAdjustment = shiftedLetter - 26;

        } else if (thisLetter < 91 && shiftedLetter < 65) {

           loopingAdjustment = shiftedLetter + 26;

        } else {

           loopingAdjustment = shiftedLetter;

        }


        let finalString = String.fromCharCode(loopingAdjustment);


        return finalString;



    });


    console.log(mappedLetters);


    return mappedLetters.join('');


}


module.exports = caesar


查看完整回答
反对 回复 2022-10-13
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

您在 let 有自己的范围的条件中声明 loopingAdjustment 。在条件句之外声明它,只在条件句中更改它的值一次。



查看完整回答
反对 回复 2022-10-13
  • 2 回答
  • 0 关注
  • 149 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号