From 72a183c0a59cdf812304243a33fdd5f3292b97a2 Mon Sep 17 00:00:00 2001 From: billalp Date: Mon, 28 Oct 2019 17:26:09 +0000 Subject: [PATCH] Capital letters are working but not in an optimal way --- caesar/caesar.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/caesar/caesar.js b/caesar/caesar.js index 6e8674d..0591ae6 100644 --- a/caesar/caesar.js +++ b/caesar/caesar.js @@ -1,19 +1,24 @@ const caesar = function(value, addBy) { - const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); + const lowerCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); + const upperCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'.toUpperCase().split(''); const specialChars = [' ', '!', ',']; - const valueLowerCase = value.toLowerCase(); - const valueArray = valueLowerCase.split(''); - + + const valueArray = value.split(''); var newArr = []; for (var i = 0; i < valueArray.length; i++) { - if (alphabet.includes(valueArray[i])) { - let letterIndex = alphabet.indexOf(valueArray[i]); + if (lowerCaseAlphabet.includes(valueArray[i])) { + let letterIndex = lowerCaseAlphabet.indexOf(valueArray[i]); let finalIndex = letterIndex + addBy; - newArr.push(alphabet[finalIndex]); + newArr.push(lowerCaseAlphabet[finalIndex]); + } else if (upperCaseAlphabet.includes(valueArray[i])) { + let letterIndex = upperCaseAlphabet.indexOf(valueArray[i]); + let finalIndex = letterIndex + addBy; + + newArr.push(upperCaseAlphabet[finalIndex]); } else if (specialChars.includes(valueArray[i])) { newArr.push(valueArray[i]); } else {