如何让数组中的元素改变它的位置
How to make element in Array change its' place
我是 JS 初学者。我试图理解 Caesar Cipher ROT13,但它对我来说太复杂了。所以我尝试编写自己的代码。下面是:
function encrip() {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var str = "Ni Hao";
var string = str.toUpperCase();
for (var i = 0; i < string.length; i++) {
for (var k = 0; k < alphabet.length; k++) {
if(string.charAt(i) == alphabet[k]) {
/* console.log(string.charAt(i) + ' ' + alphabet.indexOf(alphabet[k])); */
}
}
}
}
encrip();
但我卡住了。怎么做:
1. 从 var str 获取值然后访问 var alphabet ,在将每个字母从 var str 值更改为字母表中的下一个 3 之后(var str 每个元素的当前位置将被更改)例如:Input: Ni Hao ==> output: QL KDR
2. 创建通用代码,我的意思是,不仅要将位置改变 3,而且当我给出值“5”时,每个元素将被接下来的 5 个位置改变从字母表。所以当我改变它的值时可以改变输出
希望我解释清楚了。预先感谢大家的帮助!!
你可以使用下面的函数来加密英文单词,第一个参数是要加密的字符串,第二个参数是移位
function encryp(str,pos){
var alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var strUC=str.toUpperCase();
var enc="";
for(var i=0;i<strUC.length;i++){
if(strUC.charAt(i)!=" "){
enc+=alpha.charAt((alpha.indexOf(strUC.charAt(i))+pos)%26)
}
else{
enc+=" "
}
// in your case pos=3
}
return enc;
}
console.log(encryp("NiHao",3));
您不需要两个 for 循环来执行此操作。遍历输入 string
并找到 alphabet
数组中每个字符的索引,如果找到则将 shift
添加到其中以获得加密字符。
要处理溢出,请使用取模运算符循环遍历数组。
此外,我假设您不会使用任何特殊符号进行加密。
function encrip(string, shift) {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
string = string.toUpperCase();
let arr = [];
for (var i = 0; i < string.length; i++) {
let char = alphabet.indexOf(string[i]) !== -1 ? alphabet[(alphabet.indexOf(string[i]) %26) + shift] : " ";
arr.push(char);
}
let encryp = arr.join("");
console.log(encryp);
return encryp;
}
encrip("Ni Hao", 3);
首先,您可以使用内置函数 indexOf:
而不是内部 for
循环扫描整个 alphabet
数组
alphabet.indexOf('K') // returns 10
其次,您需要在单独的变量中构建加密字符串。对于每个字母,获取该字母在字母表中的索引,将密码偏移量参数添加到该索引并将字母表中的结果字母添加到新字符串中。一个重要的步骤是,当您添加到字母的索引时,您要确保生成的索引在 alphabet
数组的范围内。您可以使用 %
(取模)运算符来做到这一点,它将高值回绕到数组的开头。全文:
function encipher(input, offset) {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var str = input.toUpperCase();
var result = '';
for (var i = 0; i < str.length; i++) {
letterIndex = alphabet.indexOf(str.charAt(i));
if (letterIndex === -1) {
result += str[i]; // if the letter isn't found in the alphabet, add it to the result unchanged
continue;
}
cipheredIndex = (letterIndex + offset) % alphabet.length; // wrap index to length of alphabet
result += alphabet[cipheredIndex];
}
console.log(result);
}
encipher('Ni Hao', 5); // output: 'SN MFT'
我是 JS 初学者。我试图理解 Caesar Cipher ROT13,但它对我来说太复杂了。所以我尝试编写自己的代码。下面是:
function encrip() {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var str = "Ni Hao";
var string = str.toUpperCase();
for (var i = 0; i < string.length; i++) {
for (var k = 0; k < alphabet.length; k++) {
if(string.charAt(i) == alphabet[k]) {
/* console.log(string.charAt(i) + ' ' + alphabet.indexOf(alphabet[k])); */
}
}
}
}
encrip();
但我卡住了。怎么做:
1. 从 var str 获取值然后访问 var alphabet ,在将每个字母从 var str 值更改为字母表中的下一个 3 之后(var str 每个元素的当前位置将被更改)例如:Input: Ni Hao ==> output: QL KDR
2. 创建通用代码,我的意思是,不仅要将位置改变 3,而且当我给出值“5”时,每个元素将被接下来的 5 个位置改变从字母表。所以当我改变它的值时可以改变输出
希望我解释清楚了。预先感谢大家的帮助!!
你可以使用下面的函数来加密英文单词,第一个参数是要加密的字符串,第二个参数是移位
function encryp(str,pos){
var alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var strUC=str.toUpperCase();
var enc="";
for(var i=0;i<strUC.length;i++){
if(strUC.charAt(i)!=" "){
enc+=alpha.charAt((alpha.indexOf(strUC.charAt(i))+pos)%26)
}
else{
enc+=" "
}
// in your case pos=3
}
return enc;
}
console.log(encryp("NiHao",3));
您不需要两个 for 循环来执行此操作。遍历输入
string
并找到alphabet
数组中每个字符的索引,如果找到则将shift
添加到其中以获得加密字符。要处理溢出,请使用取模运算符循环遍历数组。 此外,我假设您不会使用任何特殊符号进行加密。
function encrip(string, shift) {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
string = string.toUpperCase();
let arr = [];
for (var i = 0; i < string.length; i++) {
let char = alphabet.indexOf(string[i]) !== -1 ? alphabet[(alphabet.indexOf(string[i]) %26) + shift] : " ";
arr.push(char);
}
let encryp = arr.join("");
console.log(encryp);
return encryp;
}
encrip("Ni Hao", 3);
首先,您可以使用内置函数 indexOf:
而不是内部for
循环扫描整个 alphabet
数组
alphabet.indexOf('K') // returns 10
其次,您需要在单独的变量中构建加密字符串。对于每个字母,获取该字母在字母表中的索引,将密码偏移量参数添加到该索引并将字母表中的结果字母添加到新字符串中。一个重要的步骤是,当您添加到字母的索引时,您要确保生成的索引在 alphabet
数组的范围内。您可以使用 %
(取模)运算符来做到这一点,它将高值回绕到数组的开头。全文:
function encipher(input, offset) {
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var str = input.toUpperCase();
var result = '';
for (var i = 0; i < str.length; i++) {
letterIndex = alphabet.indexOf(str.charAt(i));
if (letterIndex === -1) {
result += str[i]; // if the letter isn't found in the alphabet, add it to the result unchanged
continue;
}
cipheredIndex = (letterIndex + offset) % alphabet.length; // wrap index to length of alphabet
result += alphabet[cipheredIndex];
}
console.log(result);
}
encipher('Ni Hao', 5); // output: 'SN MFT'