Element ReAssignment Issues In a Nested For Loop

I am getting the answers I want when I try to get character code values to print to the console from the nested array, but I am not able to reassign the character codes for some reason. I have tried nested for loops and I have tried using a while loop inside of the nested for loops, but it is not doing the reassignment so that I can return the new array and concatenate it into a string with spaces…

Here is the repl Link

Here is the code snippet:

let array = [["SERR"], ["CVMMN"]];

let arr2 = [];

let text = "";

for (let i = 0; i < array.length; i++) {
  //console.log(array[i].split(""));
  for (let x = 0; x < array[i].length; x++) {
    //console.log(array[i][x].split(""));
    for (let n = 0; n < array[i][x].length; n++) {
      //console.log(array[i][x][n]);

      //console.log(array[i][x][n].charCodeAt(array[i][x][n]));

      let code = array[i][x][n].charCodeAt(array[i][x][n]);

      // console.log(code);

      let newCode;

      if (code > 77) {
        newCode = array[i][x][n].charCodeAt(array[i][x][n]) - 13;
      } else {
        newCode = array[i][x][n].charCodeAt(array[i][x][n]) + 13;
      }

      // console.log(array[i][x][n]);

      console.log(String.fromCharCode(newCode));

      let j = 0;

      while(j < array[i][x].length) {
        array[i][x][n] = String.fromCharCode(newCode);
        j++;
      }

      // array[i][x][n] = String.fromCharCode(newCode);

     console.log(array[i][x]);
  
    }
  }
}

From what I see you are trying to reassign the character to an element of a string but in JS strings are immutable. You must create a new string with the transformed characters and replace the old one.

Is this ROT13 cipher?

It is, I have the basics of the issue down, and my approach has been working up until this point. I was attempting to make the string into an array inside of a nested array and then replace the array index with the changed string from the character code…I commented out some code in the actual repl, you can see what I initially tried to do with the generate function I tried to write. It hasn’t been working, and I’ve been struggling with various iterations of the same idea since last week for this problem lol

Since it’s ROT13 try to do something different, for example, process each word in the array, calculate the ROT13 transformation for each letter in the word, build a new word from the transformed letters, and then replace the original word in the array with the new one. We can work with english letters for now and change in the future.

let array = [["SERR"], ["CVMMN"]];
let arr2 = [];

for (let i = 0; i < array.length; i++) {
  for (let j = 0; j < array[i].length; j++) {
    let word = array[i][j];
    let newWord = "";
    for (let n = 0; n < word.length; n++) {
      let code = word.charCodeAt(n);
      let newCode;
      if (code >= 65 && code <= 90) {
        // Capital letter (A-Z)
        newCode = ((code - 65 + 13) % 26) + 65;
      } else if (code >= 97 && code <= 122) {
        // Lowercase letter (a-z)
        newCode = ((code - 97 + 13) % 26) + 97;
      } else {
        // Other characters are left as is
        newCode = code;
      }
      newWord += String.fromCharCode(newCode);
    }
    array[i][j] = newWord;
  }
}

console.log(array);

1 Like

Awesome sauce! Thanks so much! I’ll see if I can re-work the logic to look more like this one.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.