作为前言,我对JS很新,所以我的错误可能是令人尴尬的基础。
我正在尝试为Atbash密码创建一个解码器,它只是反转字母表。 Z = A,Y = B,依此类推。
这就是我所拥有的:
decode("ZYX");
function decode(message) {
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var tebahpla = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
var decoded_string = "";
for (i = 0; i < message.length; i++) {
var coded_letter = message.charAt(i);
var letter_index = alphabet.indexOf(coded_letter);
var decoded_letter = tebahpla.charAt(letter_index);
decoded_string = decoded_string + decoded_letter;
}
return decoded_string;
}
答案 0 :(得分:0)
public class CifradoAtbash {
public String EnAtbash(String mensage) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String tebahpla = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
String alphabet1 = "abcdefghijklmnopqrstuvwxyz";
String tebahpla1 = "zyxwvutsrqponmlkjihgfedcba";
String decoded_string = "";
for (int i = 0; i < mensage.length(); i++) {
char coded_letra = mensage.charAt(i);
if (Character.isUpperCase(coded_letra)) {
int letraPosMayus = alphabet.indexOf(coded_letra);
char tebLetraPosMayus = tebahpla.charAt(letraPosMayus);
decoded_string = decoded_string+tebLetraPosMayus;
}
else {
int letraPosMinus1 = alphabet1.indexOf(coded_letra);
char tebLetraPosMinus1 = tebahpla1.charAt(letraPosMinus1);
decoded_string = decoded_string + tebLetraPosMinus1;
}
}
return decoded_string;
}
答案 1 :(得分:0)
我认为您可以轻松地将@Robert发布的代码从Java转换为Javascript。我已经修改了代码,以保留非字母字符(例如空格,数字,标点符号等):
function enAtbash(mensage) {
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var tebahpla = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
var alphabet1 = "abcdefghijklmnopqrstuvwxyz";
var tebahpla1 = "zyxwvutsrqponmlkjihgfedcba";
var decoded_string = "";
for (var i = 0; i < mensage.length; i++) {
var coded_letra = mensage.charAt(i);
if (/[^a-zA-Z]/.test(mensage[i])) {
decoded_string = decoded_string+mensage[i];
}
else if (mensage[i] === mensage[i].toUpperCase()) {
var letraPosMayus = alphabet.indexOf(coded_letra);
var tebLetraPosMayus = tebahpla.charAt(letraPosMayus);
decoded_string = decoded_string+tebLetraPosMayus;
} else {
var letraPosMinus1 = alphabet1.indexOf(coded_letra);
var tebLetraPosMinus1 = tebahpla1.charAt(letraPosMinus1);
decoded_string = decoded_string + tebLetraPosMinus1;
}
}
return decoded_string;
}
document.write(enAtbash("Hello World!"));