This is one is an interesting one.

So, given a string, we need to re-write the string such that each character in the string is shifted to the number of times given, according to its position in the alphabet. For example, "zoo keeper" becomes "bqq mggrgt". To me sounds like some low-level encryption of some sort.

Let's dive in:

`caesarCipher`

that takes two parameters `str`

and `num`

. The function should return the ciphered string that's passed in```
function caesarCipher(str, num){
}
```

```
function caesarCipher(str, num){
let lowerCaseStr = str.toLowerCase()
let alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
}
```

Let's go through some constraints before we go any further:

- In cases, where the number passed, is greater than the length of letters in our alphabet, we will loop through our alphabet until we land at the correct position where the loop ends.
- Wherein, when the number passed is less, we will loop backwards through the alphabet

Let's proceed

```
function caesarCipher(str, num){
let lowerCaseStr = str.toLowerCase()
let alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
let newString = ''
}
```

```
function caesarCipher(str, num){
let lowerCaseStr = str.toLowerCase()
let alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
let newString = ''
for(let i = 0; i < lowerCaseStr.length; i++){
let currentLetter = lowerCaseStr[i]
let currentindex = alphabet.indexOf(currentLetter)
}
}
```

`newString`

. We do this by defining a condition statement that determines if the `currentLetter`

is equals to an empty string and adding it to the `newString`

```
function caesarCipher(str, num){
let lowerCaseStr = str.toLowerCase()
let alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
let newString = ''
for(let i = 0; i < lowerCaseStr.length; i++){
let currentLetter = lowerCaseStr[i]
if(currentLetter === ' '){
newString += curentLetter;
continue; // This tells the loop statement to continue iterating
}
let currentindex = alphabet.indexOf(currentLetter)
}
}
```

```
function caesarCipher(str, num){
let lowerCaseStr = str.toLowerCase()
let alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
let newString = ''
for(let i = 0; i < lowerCaseStr.length; i++){
let currentLetter = lowerCaseStr[i]
if(currentLetter === ' '){
newString += curentLetter;
continue; // This tells the loop statement to continue iterating
}
let currentIndex = alphabet.indexOf(currentLetter)
let newIndex = currentIndex + num
if(newIndex > 25) newIndex = newIndex - 26
if(newIndex < 0) newIndex = newIndex + 26
if(str[i] === str[i].toUpperCase()){
newString += alphabet[newIndex].toUpperCase()
}
}
}
```

We also want to retain the Uppercase letter in our string on the ciphered string. To do this, we check if the character of our original string is equal to an uppercase character of our converted string inside the function. If true, we pass the character to our `newString`

variable and convert it to upper-case.

```
function caesarCipher(str, num){
let lowerCaseStr = str.toLowerCase()
let alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
let newString = ''
for(let i = 0; i < lowerCaseStr.length; i++){
let currentLetter = lowerCaseStr[i]
if(currentLetter === ' '){
newString += curentLetter;
continue; // This tells the loop statement to continue iterating
}
let currentIndex = alphabet.indexOf(currentLetter)
let newIndex = currentIndex + num
if(newIndex > 25) newIndex = newIndex - 26
if(newIndex < 0) newIndex = newIndex + 26
if(str[i] === str[i].toUpperCase()){
newString += alphabet[newIndex].toUpperCase()
} else
newString += alphabet[newIndex].toUpperCase()
}
}
```

There we have it! I know it's a bit complex but you've understood the concept.

See you in the next one.

Happy Hacking!

## Top comments (0)