标签:运行 clu cuug you res eject find 循环 rom
var lower = ‘abcdefghijklmnopqrstuvwxyz‘ var upper = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘ var poem = ` If by life you were deceived Don‘t be dismal, don‘t be wild In the day of grief, be mild Merry days will come, believe Heart is living in tomorrow Present is dejected here In a moment, passes sorrow That which passes will be dear ` // 准备工作 将文档分行 存入数组 var FindRow = function(s) { var array = []; for (var i = 0; i < s.length; i++) { if (upper.includes(s[i])) { console.log(`i ${i}`) for (var j = i + 1 ; j < s.length; j++) { if ((poem[j] == ‘\n‘)) { // 找回车键 切句子 console.log(`j ${j}`) var str = s.slice(i,j); console.log(`str ${str}`) array.push(str); //console.log(array) break; // 找到一个 j 之后马上打断 j循环,接着继续 i 循环 } } } } return array; } // FindRow(poem) 结果是一个包含 8条 字符串的数组; // 辅助函数 var find = function(s1,s2) { var len = s1.length; if (s1.includes(s2)) { for (var i = 0; i < len; i++) { var str = s1[i]; if (str == s2) { return i; } } } return -1; } var lowercase1 = function(s) { var result = ‘‘; for (var i = 0; i < s.length; i++) { if (upper.includes(s[i])) { var index = find(upper,s[i]); result += lower[index]; } else { result += s[i]; } } return result; } var uppercase1 = function(s) { var result = ‘‘; for (var i = 0; i < s.length; i++) { if (lower.includes(s[i])) { var index = find(lower,s[i]); result += upper[index]; } else { result += s[i]; } } return result; }
// 主函数 var encode = function(s,shift) { str = lowercase1(s) var result = ‘‘; for (var i = 0; i < str.length; i++) { if (lower.includes(str[i])) { var index = find(lower,str[i]); var next = (index + shift) % 26; result += lower[next]; }else { result += str[i]; } } return result; } var encode1 = function(s) { var n = FindRow(s); var result = ‘‘; for (var i = 0; i < n.length; i++) {
// 随机 分配 shift var shift = Math.floor(Math.random() * (25 - 0 + 1) + 0); var str = ‘\n‘ + encode(n[i],shift) ; // 第一条字符串 i = 0; 重新加在 前面 加上换行符 result += uppercase1(str); } return result; } encode1(poem)
// 运行结果(随机的)如下;
"
SP LI VSPO IYE GOBO NOMOSFON
YJI‘O WZ YDNHVG, YJI‘O WZ RDGY
BG MAX WTR HY ZKBXY, UX FBEW
ASFFM ROMG KWZZ QCAS, PSZWSJS
ROKBD SC VSFSXQ SX DYWYBBYG
VXKYKTZ OY JKPKIZKJ NKXK
KP C OQOGPV, RCUUGU UQTTQY
HVOH KVWQV DOGGSG KWZZ PS RSOF"
标签:运行 clu cuug you res eject find 循环 rom
原文地址:https://www.cnblogs.com/rencoo/p/9257766.html