JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Tell us what’s happening:
Hey guys,

Im trying to do the Ceasar’s Cipher JS project and I’m stuck on one of the steps. Im trying to parse through the alphabet array with the encryption array and return the index from the alphabet array of the elements that match. However my code is not working as intended. Any advice/feedback on my code and where I’m making a mistake would be appreciated.

Thanks,

Your code so far

function rot13(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'];
  let encryption = str.split("");
  let encryptLocation = [];
  
  for (var i in encryption){
    if (alphabet.indexOf[encryption[i]] > -1){
      encryptLocation.push(alphabet[i].indexOf());
    }
   
   }


  console.log(encryption, encryptLocation);
  return str;
}

rot13("SERR PBQR PNZC");

Your browser information:

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

Challenge: JavaScript Algorithms and Data Structures Projects - Caesars Cipher

Link to the challenge:

Could you explain in plain language how your algorithm is supposed to work?

1 Like

Do you mean [] or ()

This line looks suspicious. Its wrong in a different way from your indexOf above

it should match the elements of encryption array (query) to alphabet array (haystack) and return the index of the matched elements from the alphabet array.

ooops, thanks!
should pay more attention to detail.

ok so its populating the location array but all the returned indexes are -1

I meant explain the process your function is going to use to return the correct string. You are only explaining a part of how the function works:

This is not what the function should ultimately return. The function takes in an encoded string and returns the decoded string. So explain step by step how you go from encoded to decoded.

I’m not trying to be a jerk here. The best way to solve problems is to first understand the steps for solving them (the algorithm) and then you implement the algorithm with code.