Change shift variable and function names
Using shift as a parameter name might be confusing, since it is a verb and therefore not a good variable name on its own. Changing it to shiftValue should clear things up and also makes it possible to shorten the name of the shiftChar function to just shift, which leads to a cleaner signature: shift(char, shiftValue)
This commit is contained in:
parent
db998d7279
commit
0d27422bc5
|
@ -1,7 +1,7 @@
|
|||
const caesar = function(string, shift) {
|
||||
const caesar = function(string, shiftValue) {
|
||||
return string
|
||||
.split("")
|
||||
.map(char => shiftChar(char, shift))
|
||||
.map(char => shift(char, shiftValue))
|
||||
.join("");
|
||||
};
|
||||
|
||||
|
@ -12,12 +12,12 @@ const codeSet = code => (code < 97 ? 65 : 97);
|
|||
// https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
|
||||
const mod = (n, m) => (n % m + m) % m;
|
||||
|
||||
const shiftChar = (char, shift) => {
|
||||
const shift = (char, shiftValue) => {
|
||||
const code = char.charCodeAt();
|
||||
|
||||
if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
|
||||
return String.fromCharCode(
|
||||
mod(code + shift - codeSet(code), 26) + codeSet(code)
|
||||
mod(code + shiftValue - codeSet(code), 26) + codeSet(code)
|
||||
);
|
||||
}
|
||||
return char;
|
||||
|
|
Loading…
Reference in New Issue