(第八天)[js] 写一个加密字符串的方法

写一个加密字符串的方法

  

//加密
function strEncrypt(str) {
    return str.split('').map(s => {
      return String.fromCharCode(s.charCodeAt() + 1)
    }).join('')
  }
//解密
function outEncy(str){
    return str.split('').map(item =>{
        return String.fromCharCode(item.charCodeAt() - 1)
    }).join('');
}

let arr = 'asasd';
strEncrypt(arr);
console.log(strEncrypt(arr))
console.log(outEncy(strEncrypt(arr)))

补充一下知识点,说不定哪天就用到了,您说不是吗?

fromCharCode()方法
将 Unicode 编码转为一个字符
var n = String.fromCharCode(65);
输出A
charCodeAt()方法
将一个字符转为 Unicode 编码

 

posted @ 2021-09-01 14:53  Mr、DIVE  阅读(75)  评论(0编辑  收藏  举报