問題
我們需要編寫一個 JavaScript 函數,該函數接收字符串并根據以下算法對其進行加密 –
字符串僅包含空格分隔的單詞。
我們需要使用以下規則加密字符串中的每個單詞 –
第一個字母需要轉換為 ASCII 碼。
第二個字母需要與最后一個字母交換。
因此,根據此,字符串“good”將被加密為“103doo”。
示例
以下是代碼 –
?現場演示
const str = 'good'; const encyptString = (str = '') => { const [first, second] = str.split(''); const last = str[str.length - 1]; let res = ''; res += first.charCodeAt(0); res += last; for(let i = 2; i < str.length - 1; i++){ const el = str[i]; res += el; }; res += second; return res; }; console.log(encyptString(str));
登錄后復制
輸出
103doo
登錄后復制
以上就是使用 JavaScript 基于算法加密字符串的詳細內容,更多請關注www.92cms.cn其它相關文章!