Merge pull request #3 from BillalPatel/caeser

Capital letters are working but not in an optimal way
This commit is contained in:
Billal Patel 2019-10-28 17:26:53 +00:00 committed by GitHub
commit 6c3a7496be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 7 deletions

View File

@ -1,19 +1,24 @@
const caesar = function(value, addBy) { const caesar = function(value, addBy) {
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); const lowerCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
const upperCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz'.toUpperCase().split('');
const specialChars = [' ', '!', ',']; const specialChars = [' ', '!', ','];
const valueLowerCase = value.toLowerCase();
const valueArray = valueLowerCase.split(''); const valueArray = value.split('');
var newArr = []; var newArr = [];
for (var i = 0; i < valueArray.length; i++) { for (var i = 0; i < valueArray.length; i++) {
if (alphabet.includes(valueArray[i])) { if (lowerCaseAlphabet.includes(valueArray[i])) {
let letterIndex = alphabet.indexOf(valueArray[i]); let letterIndex = lowerCaseAlphabet.indexOf(valueArray[i]);
let finalIndex = letterIndex + addBy; 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])) { } else if (specialChars.includes(valueArray[i])) {
newArr.push(valueArray[i]); newArr.push(valueArray[i]);
} else { } else {