Javascript "variable declared but never used"...即使我正在使用它?
Javascript "variable declared but never used"... even though I'm using it?
我一定是漏了什么。
在下面的代码中,我清楚地声明了 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
您正在条件语句中声明 loopingAdjustment,其中 let 有自己的作用域。在你的条件之外声明它,只有一次并且只在条件内改变它的值。
发生这种情况是因为范围,因为您仅在 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
我一定是漏了什么。
在下面的代码中,我清楚地声明了 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
您正在条件语句中声明 loopingAdjustment,其中 let 有自己的作用域。在你的条件之外声明它,只有一次并且只在条件内改变它的值。
发生这种情况是因为范围,因为您仅在 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