問題
我們需要編寫一個 JavaScript 函數(shù),該函數(shù)接收字符串并根據(jù)以下算法對其進行加密 –
字符串僅包含空格分隔的單詞。
我們需要使用以下規(guī)則加密字符串中的每個單詞 –
第一個字母需要轉(zhuǎn)換為 ASCII 碼。
第二個字母需要與最后一個字母交換。
因此,根據(jù)此,字符串“good”將被加密為“103doo”。
示例
以下是代碼 –
?現(xiàn)場演示
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));
登錄后復(fù)制
輸出
103doo
登錄后復(fù)制
以上就是使用 JavaScript 基于算法加密字符串的詳細內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!