From 2e1f746957c2da14e37d795954a3c6354d7f9815 Mon Sep 17 00:00:00 2001 From: LaraClara Date: Fri, 5 Apr 2024 15:20:19 +1000 Subject: [PATCH] bugfix(rot13): Fix negative number rotation --- src/core/operations/ROT13.mjs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/core/operations/ROT13.mjs b/src/core/operations/ROT13.mjs index 1d059565..3194be56 100644 --- a/src/core/operations/ROT13.mjs +++ b/src/core/operations/ROT13.mjs @@ -62,20 +62,27 @@ class ROT13 extends Operation { chr; if (amount) { - if (amount < 0) { - amount = 26 - (Math.abs(amount) % 26); - } - + let amountModified; for (let i = 0; i < input.length; i++) { + amountModified = amount; chr = input[i]; if (rot13Upperacse && chr >= 65 && chr <= 90) { // Upper case - chr = (chr - 65 + amount) % 26; + if (amountModified < 0) { + amountModified = 26 - (Math.abs(amountModified) % 26); + } + chr = (chr - 65 + amountModified) % 26; output[i] = chr + 65; } else if (rot13Lowercase && chr >= 97 && chr <= 122) { // Lower case - chr = (chr - 97 + amount) % 26; + if (amountModified < 0) { + amountModified = 26 - (Math.abs(amountModified) % 26); + } + chr = (chr - 97 + amountModified) % 26; output[i] = chr + 97; } else if (rotNumbers && chr >= 48 && chr <= 57) { // Numbers - chr = (chr - 48 + amount) % 10; + if (amountModified < 0) { + amountModified = 10 - ((Math.abs(amountModified)) % 10); + } + chr = (chr - 48 + amountModified) % 10; output[i] = chr + 48; } }