根據某種演算法對基於程式碼的源訊息在 JavaScript 中進行解密


問題

我們需要編寫一個 JavaScript 函式,它接收一個解密後的訊息並返回其源訊息。

我們已知用於加密該訊息的演算法。

並且演算法為 −

  • 反轉訊息字串。
  • 用引號中的 ASCII 程式碼替換每個字母(A 到 '65',h 到 '104',依此類推)。
  • 按原樣插入數字和空格。

示例

以下為程式碼 −

 動態演示

const str = '12 hello world 30';
const decryptString = (str = '') => {
   const alpha = 'abcdefghijklmnopqrstuvwxyz';
   let res = '';
   for(let i = str.length - 1; i >= 0; i--){
      const el = str[i];
      if(alpha.includes(el.toLowerCase())){
         res += `'${el.charCodeAt(0)}'`;
      }else{
         res += el;
      };
   };
   return res;
};
console.log(decryptString(str));

輸出

以下為控制檯輸出 −

03 '100''108''114''111''119' '111''108''108''101''104' 21

更新於: 2021 年 4 月 19 日

109 次瀏覽

開啟你的 職業 生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.