基本上我正在研究这个coding challenge在 FreeCodeCamp 上。挑战的开头提到了这一点......
A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.
Write a function which takes a ROT13 encoded string as input and returns a decoded string.
到目前为止,我已将字符串传递给数组,字符串的每个部分都在其自己的数组(子数组)中。从那里我想我将使用一个循环来遍历字符串中的每个字符(在每个子数组中)并将 CharCodeAt 方法传递给每个字符。
我的问题在于将每个单独的字符传递给 CharCodeAt。
这是整个功能的一部分,如下所示:
对我来说这是有道理的,因为我将每个子数组值传递到变量中
var foo = container[i][k];
// var output = container.map(function mapHandler(){foo.charCodeAt(k)});
function rot13(str){
var container, j = 0;
while(j <= str.length - 1){
container = str.split(' ');
j++;
}
for(var i = 0; i < container.length; i++){
for(var k = 0; k < container[i].length; k++){
var foo = container[i][k];
var map = Array.prototype.map;
var output = map.call(foo, function(x){ return x.charCodeAt(k);});
// var output = container.map(function mapHandler(){foo.charCodeAt(k)});
// output.charCodeAt(k);
}
}
return output;
}
rot13("SERR PBQR PNZC") should decode to "FREE CODE CAMP"
任何人都可以帮助实现这一点吗?我不需要整个解决方案!
提前致谢!
最佳答案
您可以通过用空字符串将字符串拆分为单个字母数组:
var arr = str.split("")
然后您可以使用例如 .map()
将每个字母更改为解码后的字母:
var decoded = arr.map(x=> {
// decode x
})
关于javascript - 在 JavaScript 中将子数组的值传递给 CharCodeAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36519499/