I must be missing something.
In the below code, I am clearly declaring loopingAdjustment
and then right below it I'm calling it in the fromCharCode
function. So, I'm using it, right? I should be able to call it because it's in the same scope, right?
Why is Visual Studio Code saying it's "never used", and why is my terminal saying it's "not defined"?
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
This is happening because of scope, as you have declared and defined loopingAdjustment variable inside if statement only, so scope is limited to if statement. To fix this you can declare loopingAdjustment outside if and assign it inside if statement. You can take reference from below code and you can modify acc. to your needs.
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