凯撒密码(我如何将 for 转换为 .map)
Caesar Cipher (How do I convert for into .map)
这是一个凯撒密码程序,我自己写了这段代码,想把这个for循环转换成.map JavaScript内置函数,我试了很多次都搞不懂.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
这个网站上有很多关于 .map 的问题,但这对我不起作用。
function rot13(str) {
var chArray = [];
str = str.split("");
for(var i in str){
var char = str[i].charCodeAt(0);
if(/[A-M]/g.test(str[i])){
chArray.push(char + 13);
}
else if(/[N-Z]/g.test(str[i])){
chArray.push(char - 13);
}
else chArray.push(char);
}
str = String.fromCharCode.apply(String,chArray);
return str;
}
rot13("SERR PBQR PNZC");
另一种选择是使用 array.map here.
映射方法接收一个回调,该回调将数组中的每个元素作为参数。
示例:
var arr = [1,2,3,4,5];
arr.map(someFunc);
var someFunc = function(currentItem){
console.log(currentItem);
//This will output 1
//2
//...
}
因此,您可以在传递给 map 的 someFunc 中定义密码转换。
假设您要对以下字符串进行编码
var secretMessage = "hello"
msgArray = secretMessage.split("");
msgArray.map(cypher);
var cypher = function(i){
///doStuff
}
下面是对您的代码的简单改编以使用 map
(使用变量 rotation
以避免重复代码):
function rot13(str) {
str = str.split("");
var encryptedChArray = str.map(char => {
var rotation = 0;
if(/[A-M]/g.test(char)){
rotation = 13;
}
else if(/[N-Z]/g.test(char)) {
rotation = -13;
}
return char.charCodeAt(0) + rotation;
});
return String.fromCharCode.apply(String, encryptedChArray);
}
console.log(rot13("SERR PBQR PNZC"));
您可以将字符串拆分为一个字符数组,然后对其进行映射,执行转换,最后,将转换后的字符数组连接回一个字符串。
function rot13(str) {
// get the char code for the character
const charCode = str.charCodeAt(0);
if(/[A-M]/g.test(str)){
return String.fromCharCode(charCode + 13);
}
else if(/[N-Z]/g.test(str)){
return String.fromCharCode(charCode - 13);
}
else {
return String.fromCharCode(charCode);
}
}
// first split the string into an array of single characters
const newString = "SERR PBQR PNZC".split("").map(rot13).join('');
console.log(newString);
如果你想做的非常紧凑,你可以这样做:
const A = 'A'.charCodeAt(0)
const M = 'M'.charCodeAt(0)
const N = 'N'.charCodeAt(0)
const Z = 'Z'.charCodeAt(0)
function rot13(str) {
var chArray = [];
return String.fromCharCode(...(
str.split('')
.map(c => c.charCodeAt(0))
.map(c =>
(A <= c && c <= M) ? c + 13 :
(N <= c && c <= Z) ? c - 13 :
c
)
))
}
console.log(rot13("SERR PBQR PNZC"))
这是一个凯撒密码程序,我自己写了这段代码,想把这个for循环转换成.map JavaScript内置函数,我试了很多次都搞不懂.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map
这个网站上有很多关于 .map 的问题,但这对我不起作用。
function rot13(str) {
var chArray = [];
str = str.split("");
for(var i in str){
var char = str[i].charCodeAt(0);
if(/[A-M]/g.test(str[i])){
chArray.push(char + 13);
}
else if(/[N-Z]/g.test(str[i])){
chArray.push(char - 13);
}
else chArray.push(char);
}
str = String.fromCharCode.apply(String,chArray);
return str;
}
rot13("SERR PBQR PNZC");
另一种选择是使用 array.map here.
映射方法接收一个回调,该回调将数组中的每个元素作为参数。
示例:
var arr = [1,2,3,4,5];
arr.map(someFunc);
var someFunc = function(currentItem){
console.log(currentItem);
//This will output 1
//2
//...
}
因此,您可以在传递给 map 的 someFunc 中定义密码转换。
假设您要对以下字符串进行编码
var secretMessage = "hello"
msgArray = secretMessage.split("");
msgArray.map(cypher);
var cypher = function(i){
///doStuff
}
下面是对您的代码的简单改编以使用 map
(使用变量 rotation
以避免重复代码):
function rot13(str) {
str = str.split("");
var encryptedChArray = str.map(char => {
var rotation = 0;
if(/[A-M]/g.test(char)){
rotation = 13;
}
else if(/[N-Z]/g.test(char)) {
rotation = -13;
}
return char.charCodeAt(0) + rotation;
});
return String.fromCharCode.apply(String, encryptedChArray);
}
console.log(rot13("SERR PBQR PNZC"));
您可以将字符串拆分为一个字符数组,然后对其进行映射,执行转换,最后,将转换后的字符数组连接回一个字符串。
function rot13(str) {
// get the char code for the character
const charCode = str.charCodeAt(0);
if(/[A-M]/g.test(str)){
return String.fromCharCode(charCode + 13);
}
else if(/[N-Z]/g.test(str)){
return String.fromCharCode(charCode - 13);
}
else {
return String.fromCharCode(charCode);
}
}
// first split the string into an array of single characters
const newString = "SERR PBQR PNZC".split("").map(rot13).join('');
console.log(newString);
如果你想做的非常紧凑,你可以这样做:
const A = 'A'.charCodeAt(0)
const M = 'M'.charCodeAt(0)
const N = 'N'.charCodeAt(0)
const Z = 'Z'.charCodeAt(0)
function rot13(str) {
var chArray = [];
return String.fromCharCode(...(
str.split('')
.map(c => c.charCodeAt(0))
.map(c =>
(A <= c && c <= M) ? c + 13 :
(N <= c && c <= Z) ? c - 13 :
c
)
))
}
console.log(rot13("SERR PBQR PNZC"))