Topic describes

Their thinking

  1. First, each character is iterated.
  2. Use charCodeAt() to determine the ASCII code value of A character, if the value is in the range after -13 between A and Z, and if it is in the range, use charCodeAt() to convert the character directly.
  3. If the value is not between A and Z, simply concatenate the original characters.

The implementation code

function rot13(str) {
    let temp = ' ';
    for (let v of str) {
        if (v.charCodeAt() < 65 || v.charCodeAt() > 90) {
            temp = temp + v
        } else {
            if (v.charCodeAt()-13 < 65) {
                temp = temp + String.fromCharCode(91 - (65 - v.charCodeAt() + 13))}else {
                temp = temp + String.fromCharCode(v.charCodeAt()-13)
            }
        }
    }
    temp
    return temp;
}

rot13("SERR CVMMN!");
Copy the code

Topic link

Caesar password

The title to reflect

  • Learn to use charCodeAt() to convert characters to their corresponding ASCII values.
  • Learn to convert ASCII values to their corresponding characters using fromCharCode.
  • Learn how to use new maps to convert an array into a Map, which is much easier than adding them to a Map one by one.