Home > Net >  Need help building a caesar cipher project
Need help building a caesar cipher project

Time:02-03

I have a code, that takes a string, it converts to array and using split(''), i have a for loop that search the element in the alphabet array i created, i need to change the index of the element searched

Here is the code :

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];


function caesar (str, plusIndex) {

  for (let i = 0; i < str.length; i  ) {
    let divided = str.split('') // split the string into a array
    let indexes = alphabet.indexOf(divided[i]); //shows the index of each divided element in alphabet

  }

}

caesar("hey") // this show 7,4,24 in each iteration

I need to change this like caesar("hey", 3) show something like 10,7,27 and return the element of that indexes hey,3 should output m,j,d

i tried using another string, charCodeAt, charAt, but i cant convert the index into a different index

CodePudding user response:

Considering you're only using lower case alphabets. You don't need an array, you can just use the ascii numbers. (97 - 122 => a - z)
Then in your function caeser(string, key) split the string as you currently are, and for each letter =>

int offset = (int(letter)   key)&;
int cipher = char(97 (26-offet-1));  

cipher is your required letter.
Now 97 is position of a in ascii. 26 is number of alphabets. The -1 in offset - 1 is done to include the 97th ascii position ( a ) in calculation

CodePudding user response:

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];


function caesar (str, plusIndex) {

  
    let encrypted = [] //create an array to store the new word
    for (let i = 0; i < str.length; i  ) {
        let divided = str.split('')
        let indexes = (alphabet.indexOf(divided[i])   plusIndex)%alphabet.length; //Get the index of the current letter, add the shift and loop back to start if the index is out of bounds
        encrypted.push(alphabet[indexes]) //add the new letter to the encrypted array
      }
    return(encrypted)
    }
    caesar("hey", 8)

Be aware that this will only work for characters that are in your "alphabet" variable.

CodePudding user response:

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

function caesar (str, plusIndex) {
  let ans = ""
  for (let i = 0; i < str.length; i  ) {
    let oldIndex = alphabet.findIndex(item => item === str[i])
    let newIndex = (oldIndex   plusIndex) % alphabet.length
    ans = ans   alphabet[newIndex]
  }
  return ans
}

let ans = caesar("hey",3) 

console.log(ans) #shows khb

if you want just indexes use this:

let alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];

function caesar (str, plusIndex) {
  let ans = ""
  for (let i = 0; i < str.length; i  ) {
    let oldIndex = alphabet.findIndex(item => item === str[i])
    let newIndex = (oldIndex   plusIndex) % alphabet.length
    ans = ans   newIndex   " "
  }
  return ans
}

let ans = caesar("hey",3) 

console.log(ans) # shows 10 7 1 
  • Related