DEV Community

Clint Maruti
Clint Maruti

Posted on

Caesar Cipher - Challenge 3

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:

  • We will define a function caesarCipher that takes two parameters str and num. The function should return the ciphered string that's passed in

  • function caesarCipher(str, num){
    
    }
    
    Enter fullscreen mode Exit fullscreen mode
  • Next, we will, of course, convert the string into lowercase for easier manipulation throughout our function. We will store our new lowercase string in a variable. Below that, we can as well define a variable array and pass in all the alphabet letters

  • function caesarCipher(str, num){
       let lowerCaseStr = str.toLowerCase()
       let alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
    }
    
    Enter fullscreen mode Exit fullscreen mode

    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

  • We define an empty variable to store our ciphered string

  • function caesarCipher(str, num){
       let lowerCaseStr = str.toLowerCase()
       let alphabets = 'abcdefghijklmnopqrstuvwxyz'.split('')
       let newString = ''
    }
    
    Enter fullscreen mode Exit fullscreen mode
  • Next, we loop through our string and locate the index of every character of our string in the alphabet so that we can shift it and determine the new character.

  • 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) 
       }
    }
    
    Enter fullscreen mode Exit fullscreen mode
  • So, we also want to pass the empty spaces to our 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) 
       }
    }
    
    Enter fullscreen mode Exit fullscreen mode
  • We will define another variable and set it to the new index after adding the number passed in. While at it, we will set our constraints.

  • 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()
          }
       }
    }
    
    Enter fullscreen mode Exit fullscreen mode

    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()
       }
    }
    
    Enter fullscreen mode Exit fullscreen mode

    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)