-1

I created an array with capital letters to corresponding Ceasar cipher letters so A the first would move 13 places and corespond to N the 14th letter. I want to write a code than can output all of these things but I made a mistake somewhere. can anyone help me? and explain briefly what I my mistake was?

let abcToCesar = [
  {abc: "A", cesar: "N"},
  {abc: "B", cesar: "O"},
  {abc: "C", cesar: "P"},
  {abc: "D", cesar: "Q"},
  {abc: "E", cesar: "R"},
  {abc: "F", cesar: "S"},
  {abc: "G", cesar: "T"},
  {abc: "H", cesar: "U"},
  {abc: "I", cesar: "V"},
  {abc: "J", cesar: "W"},
  {abc: "K", cesar: "X"},
  {abc: "L", cesar: "Y"},
  {abc: "M", cesar: "Z"},
  {abc: "N", cesar: "A"},
  {abc: "O", cesar: "B"},
  {abc: "P", cesar: "C"},
  {abc: "Q", cesar: "D"},
  {abc: "R", cesar: "E"},
  {abc: "S", cesar: "F"},
  {abc: "T", cesar: "G"},
  {abc: "U", cesar: "H"},
  {abc: "V", cesar: "I"},
  {abc: "W", cesar: "J"},
  {abc: "X", cesar: "K"},
  {abc: "Y", cesar: "L"},
  {abc: "Z", cesar: "M"},
]

function toCaesar(str) {
  for(let i = 0; i < str.length-1; i++){
      for(let i = 0; i < abcToCesar.length; i++){
          if(str.charAt(i) == abcToCesar[i].abc){
              str.charAt(i) == abcToCesar[i].cesar
          }
          else{
              return str.charAt(i)
          }
      }
  }
  return str;
}

console.log(toCaesar("SERR PBQR PNZC"));
David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    In what way is your code not working as expected? Please elaborate on the specific problem you are observing and what debugging you have done. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David May 17 '23 at 17:05
  • 1
    You seem to be returning in the middle of your loop, which may end the function earlier than expected. – mykaf May 17 '23 at 17:05
  • 1
    *At a glance*... There's a `return` statement inside of a loop. So if you reach that statement, your function will return and will not finish the loop. Was that intentional for the logic you're trying to build? – David May 17 '23 at 17:07
  • *Additionally*... The statement `str.charAt(i) == abcToCesar[i].cesar` in your `if` block does nothing. Did you mean to *assign* instead of *compare*? Though that wouldn't do what you expect either. Overall, this is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? – David May 17 '23 at 17:09
  • You also have two different variables called `i` in the same scope. Honestly, the more I look at this code, the more typos and mistakes I find. Now is probably a good time for you to start over. Don't try to write the whole thing in one attempt. Build one small piece of functionality at a time, focusing on the fundamentals of what you're building and of the JavaScript language itself. Validate each piece of functionality before trying to add the next one. – David May 17 '23 at 17:11
  • 2
    You really should have used an object or map and not an array of objects. `const lookup = { A: 'N', B: 'O', C: 'P' }; const str = 'ABC'; const result = Array.from(str, x => lookup[x] || x).join(''); console.log(result);` – epascarello May 17 '23 at 17:13

1 Answers1

0

First, strings are immutable in JavaScript, so you cannot assign a new character to a particular index. (And you're using == which does comparison rather than assignment anyway.) You can construct a new string while iterating over the original string instead. A simple method is to use String#replace to just handle the uppercase letters.

Moreover, there is no need to list every single letter. Uppercase ASCII letters have consecutive ASCII values so you can just add 13 to the char code, then convert it back to a string.

function toCaesar(str) {
  const A = 'A'.charCodeAt();
  return str.replace(/[A-Z]/g, c => String.fromCharCode(A + (c.charCodeAt() - A + 13) % 26));
}

console.log(toCaesar("SERR PBQR PNZC"));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80