JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Tell us what’s happening:
I’m trying to find out why my special character regex isn’t working.
I’ve tried a lot of different regex patterns here but none seem to work on the whitespaces and special characters.

Is there something that blocks me from using regex here?
I can get code working by hard coding all the special characters in tests but that surely is not what is sought after.

Your code so far

function rot13(str) {
  //create variables for alphabet and empty string
  let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let deciphered = "";
  //added regex variable for readability
  let specialsRegex = /[^A-Z]/g

  //loop through the string and find letter location on alphabet
  for (let i = 0; i < str.length; i++) {
  //add spaces or other special characters (figured out I should be able to use regex here)
    if (str[i] == specialsRegex) {
      deciphered += str[i];
    }
    for (let j = 0; j < alphabet.length; j++) {
      if (str[i] === alphabet[j]) {
        if (j + 13 < alphabet.length) {
          deciphered += alphabet[j + 13];
        } else {
          deciphered += alphabet[j - 13];
        }
      }
    }
  }
  
  console.log(deciphered)
  
  return deciphered;
}

rot13("SERR PBQR PNZC");
rot13("SERR CVMMN!");
rot13("SERR YBIR?");
rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Link to the challenge:

    if (str[i] == specialsRegex) {

The line above is checking where a character in the string is equal to a regular expression. These will NEVER be equal. What did you think this was checking or hoping to check?

Hello.
I have run your code and found this line,

if (str[i] == specialsRegex)

to be the root problem.

Before seeing the solution, try to understand why it is not working out.

Logging the value of variables will always help you to see what’s going on, i.e., using console.log().

You are comparing a String ("some text") with a RegExp object (/[^A-Z]/g).
The end result of such comparison will always be false.

TIP: Strings have a method called match. Regular expressions have a method called test. One or the other can help you achieve what you need.

I thought that would check if the value in str[i] would be found in the regex as the loops do.

But why? str[i] having a value of space is outside of the regex boundaries.
Shouldn’t that evaluate to true?

Is str[i] a regular expression? If it is a string, then it will never be a regular expression, even if it may match one.

1 Like

It returns false because you are comparing different data types. You are asking: is this string equal to this regular expression object?

Try to console.log():
String == RegExp
String == Array
String == Number

@andrebdinis

It is great that you solved the challenge, but instead of just giving a solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

I’ve solved the challenge but I kind of had to resort in hard coding some regex cases.

I was not able to make a single regex that would cover:
1 555-555-5555 = true
1 (555) 555-5555 = true
1(555)555-5555 = true
1 555 555 5555 = true
1 555)555-5555 = false

So I made regex cases:

//1 555 555 5555
/(?:^1\(\d{3}\)\d{3}\-\d{4})/,

//1 (555) 555-5555
/^1 \(\d{3}\) \d{3}\-\d{4}/,

//1 555-555-5555
/^1 \d{3}\-\d{3}\-\d{4}/

It makes me feel like I took a shortcut there and I’m really interested in getting some advice what should I do to match more cases.
I’m sure there is a way to cover all the example cases I mentioned with a single regex because at one point I only had one test case left to tackle but the regex got so complicated I totally lost track what happens when and where.

this in itself is a reason not to bother with “one regex to rule them all”! (I hope you are a Lord of the Rings fan…)

You can use the or character | to separate different groups or you can use lookaheads but in the end, if your regex is unreadable to you, then that is not useful.

1 Like

@antisankari Please do not ask a question that is for another challenge on the same thread. Please start another topic for your other challenge question.

Thank you.

Oh man, sorry. I didn’t mean to mix things up.
I mixed up the threads as both of the last threads handle regex.

No worries. When users are searching for help with specific challenges, the topic title is a key part of that search.

Roger that. I will rectify my answer.