Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

for loop - JavaScript Letter replacing code, not replacing capitals?

I am trying to complete a codewars challenge as my practice coding since I will have a beginner tech test to enter a coding training program. In case you would like to know what the challenge is: https://www.codewars.com/kata/530e15517bc88ac656000716/train/javascript

I have written code which does what is expected. I will quote Codewars.com below:

ROT13 is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher.

Create a function that takes a string and returns the string ciphered with Rot13. If there are numbers or special characters included in the string, they should be returned as they are. Only letters from the latin/english alphabet should be shifted, like in the original Rot13 "implementation".

My code grabs the test string "grfg" and converts it to the word "test" which would be the equivalent to 13 letters ahead in the alphabet, however, if I pass the string as "Grfg" with the capital "G"it returns "gest" meaning that it will not replace capital letters.

If I pass "test" in lower case it will return "grfg", so it works backwards too, however, if I pass "Test" it will return "trfg" not replacing the capital again but returning the same letter.

Please find the code I wrote below: (Please keep in mind that I am starting to code, so there are probably more than a couple way more efficient pieces of code to do this.)

    function rot13(message){
      let abc = ["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",   "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 msg = message.split("");
    
      for (let i = 0; i < abc.length; i++){
        for (let j = 0; j < abc.length; j++) {
            if (msg[j] === abc[i]) {
              
              ind = parseInt(abc.indexOf(i), 0) + 13;
              msg[j] = abc.slice(i + 13, i + 14);
                   
          };
        };
      };
      
      return msg.join("").toLowerCase();
    };
    
    rot13("test");

Would you please help me figure out what my mistake is or what I should know to make sure that my code will convert the strings regardless of capitals or lowercase?

Thanks a lot in advance :D

question from:https://stackoverflow.com/questions/66056813/javascript-letter-replacing-code-not-replacing-capitals

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Have you tried making the message all lower-case first, before splitting it?

let msg = message.toLowerCase().split("");


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...