1

I'm beginner in JS. I've tried to understand Caesar Cipher ROT13, but it was too complicated for me. So I've tried to write my own code. Here it is below:

function encrip() {
  var 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"];
  var str = "Ni Hao"; 
  var string = str.toUpperCase();
    for (var i = 0; i < string.length; i++) {
        for (var k = 0; k < alphabet.length; k++) {
            if(string.charAt(i) == alphabet[k]) {
         /* console.log(string.charAt(i) + ' ' + alphabet.indexOf(alphabet[k])); */
        }
        }
    }
}
encrip();

But I am stuck. How to do:

1. Get value from var str and then access to var alphabet , after change each letter from var str value to next 3 from alphabet (var str each element's current position would be changed) For example: Input: Ni Hao ==> output: QL KDR

2. Create universal code, I mean, not only for changing position by 3, but when I give value '5', each element would be changed by next 5 positions from alphabet. So output can be changed when I change its' value

I hope I explained everything clearly. Thanks everyone in advance for help!!

Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
Vagif Aghayev
  • 391
  • 1
  • 3
  • 13

3 Answers3

0

you can use the following function to encrypt english words, the 1st parameter is the string to encrypt and the 2nd for shifting

function encryp(str,pos){
   var alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var strUC=str.toUpperCase();
   var enc="";
    for(var i=0;i<strUC.length;i++){
    if(strUC.charAt(i)!=" "){
    enc+=alpha.charAt((alpha.indexOf(strUC.charAt(i))+pos)%26)
     }
  else{
   enc+=" "
     }
   // in your case pos=3
    }
    
    return enc;
    }
console.log(encryp("NiHao",3));
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164
0
  • You don't need two for loops to do this. Iterate over the input string and find the index of each character in the alphabet array, if found add the shift to it to get the encrypted character.

  • To handle overflow use the modulus operator to cycle through the array. Also I assume that you are not going use any special symbols to do the encryption.

function encrip(string, shift) {
  var 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"];
  string = string.toUpperCase();
  let arr = [];
    for (var i = 0; i < string.length; i++) {
       let char = alphabet.indexOf(string[i]) !== -1 ? alphabet[(alphabet.indexOf(string[i]) %26) + shift] : " ";
       arr.push(char);
    }
  let encryp = arr.join("");
  console.log(encryp);
  return encryp;
}
encrip("Ni Hao", 3);
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
-1

First of all, instead of your inner for loop scanning the whole alphabet array, you can use the built-in function indexOf:

alphabet.indexOf('K') // returns 10

Secondly, you'll want to build up your enciphered string in a separate variable. For each letter, get the index of that letter in the alphabet, add your cipher offset parameter to that index and add the resulting letter from the alphabet to your new string. An important step is that when you add to the index of the letter, you want to make sure the resulting index is within range for the alphabet array. You can do that using the % (modulo) operator, which will wrap high values back round to the start of the array. In full:

function encipher(input, offset) {
    var 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"];
    var str = input.toUpperCase();
    var result = '';

    for (var i = 0; i < str.length; i++) {
        letterIndex = alphabet.indexOf(str.charAt(i));
        if (letterIndex === -1) {
            result += str[i]; // if the letter isn't found in the alphabet, add it to the result unchanged
            continue;
        }

        cipheredIndex = (letterIndex + offset) % alphabet.length; // wrap index to length of alphabet
        result += alphabet[cipheredIndex];
    }

    console.log(result);
}

encipher('Ni Hao', 5); // output: 'SN MFT'
Igid
  • 515
  • 4
  • 15