我希望将一个字符串(例如:0.1.2)转换为char字符串(它变为:abc)。错误在哪里? 谁能帮我。 谢谢!
let alphacode = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27];
let 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'];
function uncrypt(stringToUncrypt) {
let temp = new Array();
temp = stringToUncrypt.split(".");
for (let i = 0; i < temp.length; i++) {
//console.log('Questa è la stringa senza il PUNTO: ' + temp[i]);
}
//console.log('contenuto di temp: ' + temp);
let result = [];
//console.log('contenuto di result: ' + result);
for (let j in temp) {
//console.log('siamo dentro let j in temp:' + temp[j]);
for (let x in alphacode) {
//console.log('siamo dentro let x in alphacode:' + alphacode[x]);
for (let z in alphabet) {
if (temp[j] === alphacode[x]) {
alphacode[x] = alphabet[z];
console.log('contenuto di alphabet [z]' + alphabet[z]);
result += alphabet[z];
}
}
}
}
console.log('----------UNCRYPTED----------')
return 'Risultato: ' + result;
}
let text = uncrypt('0.1');
console.log(text);
答案 0 :(得分:2)
仅对代码进行少量修改。您太复杂了。
let 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'];
function uncrypt(stringToUncrypt) {
const temp = stringToUncrypt.split("."); // no need to initialize the variable to an empty array, just set it to the splitted string directly
let result = ''; // as you want a string as output, it makes sense to directly work with strings here
for (let j of temp) { // use for..of to iterate over the characters inside the array, e.g. "0", "1"
result += alphabet[parseInt(j)]; // take the char as a position in the alphabet, e.g. alphabet[0] -> "a"
}
console.log('----------UNCRYPTED----------')
return 'Risultato: ' + result;
}
let text = uncrypt('0.1');
console.log(text);
在我的解决方案中,存储在temp
中的数字对应于alphabet
数组的索引。
答案 1 :(得分:0)
let alphacode = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27'];
let 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'];
function uncrypt(decodedString) {
const chars = decodedString.split(".");
return chars.map(char => alphabet[alphacode.indexOf(char)]).join("");
}
let text = uncrypt('0.1');
console.log('Risultato: ' + text);
map
帮助遍历数组,并用alphacode
中的匹配代码替换并返回当前值。
答案 2 :(得分:0)
由于您在代码result
中声明的是一个数组,因此需要将答案push
放入其中,而不是像字符串那样将其串联起来:
result.push(alphabet[z]);
然后,要使用正确的格式获取从函数返回的字符串,应使用join
:
return 'Risultato: ' + result.join('');
但是,您的代码中使用了太多的循环。您只需要一个:
let alphacode = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27'];
let 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'];
function uncrypt(stringToUncrypt) {
let temp = stringToUncrypt.split(".");
let result = [];
// loop over the split string ONLY
for (let i = 0; i < temp.length; i++) {
// Get in the index of the current element in the
// alphacode array
const index = alphacode.indexOf(temp[i]);
// Push the corresponding element with that index from
// alphabet into the result array
result.push(alphabet[index]);
}
return 'Risultato: ' + result.join('');
}
let text = uncrypt('0.1');
console.log(text); // ab
答案 3 :(得分:-2)
function uncrypt(stringToUncrypt) {
let 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'];
return stringToUncrypt.split(".").map(i => alphabet[i-1]).join('');
}
做你想要的事的简短方法