Jump to content

Caesars Cipher, how to use regex to add in spaces and other non-characters?

mrchow19910319

I am pretty much solved this algorithm challenge, but I do not know how to use regex to add in the spaces.
Here is my code :

 

/*

requirment: if input is a letter => shift 13 places then output the result
						if input is a space, output space

*/
function rot(str) {
    const 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 input_num = 0;
    var output_num = 0;
    var output = "";
    //outer loop for str
    for (let i = 0; i < str.length; i++) {
        //inner loop for ALPHABET
        for (let j = 0; j < ALPHABET.length; j++) {
            if (ALPHABET[j] === str[i]) {
                input_num = j;
                output_num = input_num + 13;
                if (output_num > 25) {
                    output_num = output_num - 26;
                }
                output = output + ALPHABET[output_num];
            }
        }
    }
    console.log(output);
}

rot("SERR PBQR PNZC");

 

If it is not broken, let's fix till it is. 

Link to comment
Share on other sites

Link to post
Share on other sites

Why not check if the character is a space first before shifting it? 

 

P.s. I'd recommend looking into the modulo operator (%), it's handy for taking care of overruns.

 

P.p.s. characters are also equivalent to their ascii code (A = 65, Z = 90, a = 97, z=122), so you don't actually need to create an alphabet, you can just add to the character.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, James Evens said:

              if (output_num > 25) {
                    output_num = output_num - 26;
                }
                output = output + ALPHABET[output_num];

 If you have output_num=25 it will afterwards -1 and it will crash your program.

damn it...

If it is not broken, let's fix till it is. 

Link to comment
Share on other sites

Link to post
Share on other sites

A fun challenge,

 

And nice work, but your code can be improved in a number of ways.

 

You also implemented your own search function for an array, nice practice but javascript already has a prototype for this .indexOf(). It is good you want to code and try new things, but it is also not needed to reinvent the weel.

 

In your code you also use a lot of "magic numbers", you should always try to prevent that as much as possible, because if you look back at it after a while you don't know what all the numbers are and what they do. Also what numbers do you need to change if all of a sudden your alphabet changes. If the variable is a property of another object just use that like the array length in this case.

 

I found it an interesting challenge so I had a go at it myself, I created a function for the shift of the characters, this allows you to uses this part of the projects in others if need be. It also allows you to easily expand it. In my case I made it so it can take any shift, positive or negative.

 

The while loops allows for shifts larger than the length of the array. There are other/better ways to do this but I kinda forgot that it was an option to have larger numbers and this way I only needed to change out if for while.

 

This code also solves the problem with spaces, because spaces aren't in the alphabet the function just returns the character.

 

const Charset = ["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"];
const CharShift = 13;

function shift(input, shift) {
    let i = Charset.indexOf(input);
    if(i === -1) {
        return input;
    }
    let pos = i + shift;

    while(pos >= Charset.length) {
        pos -= Charset.length;
    }
    while(pos < 0) {
        pos += Charset.length;
    }
    return Charset[pos];
}

function rot(str) {
    console.log(str);
    let output = "";
    for (let i = 0; i < str.length; i++) {
        output += shift(str[i], CharShift);
    }
    console.log(output);
}

rot("SERR PBQR PNZC");

 

Link to comment
Share on other sites

Link to post
Share on other sites

I figured out a way to add in all the non letter characters and made it work.

I will study your solution later, if I find anything I dont understand, I will tag you here!

 

Thanks!!!

If it is not broken, let's fix till it is. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×