尝试编写一个加密程序,加密方法将明文的字母变成其后的第4个字母 例如输入"China", 输出密文"Glmre", 输入 "ab123", 输出 "ef123", 输入 "a*#c", 输出 "e*#g"
function convert(str: string): string {
const field = "abcdefghijklmnopqrstuvwxyz";
const reg = /^[A-Z]+$/;
const findField = (s:string)=>{
const isUpper = reg.test(s) // 判断是否大写
const lowerStr= isUpper ? s.toLowerCase() : s // 大写的话转小写
const index = field.indexOf(lowerStr) // 找到下标
const resultIndex = index < 22 ? index + 4 : index - 22 // 下标加4
const result = index === -1 ? s : field[resultIndex] // 取值
return isUpper ? result.toUpperCase() : result // 大写转回小写
}
const result = str.split('').map(i=>findField(i))
return result.join('');
}