将字母编码为数字

时间:2019-03-03 17:08:24

标签: javascript string decode encode

我觉得我这学期的所有课程都不及格。但我想知道是否所有人都可以帮助我进行JS项目。我们的任务是使用HTML中的textareas本质上将数字转换为字母,反之亦然。我能够将数字转换为字母,但是却遇到其他困难。我所有的是:

var $ = function(id) {
return document.getElementById(id);
};
window.onload = function() {
    $("btnDecode").onclick = fnDecode;
    $("btnEncode").onclick = fnEncode;
    $("btnClear").onclick = fnClear;
};
function fnDecode() {
var msg = $("textin").value;
if (msg === "") {
    $("textin_span").innerHTML = "* Please enter a message to decode *";
    $("textin").focus;
    return;
} else {
    $("textin_span").innerHTML = "";
}
var nums = msg.split(",");
var outstr = "";

for(var i=0; i < nums.length; i++) {
    var n2 = parseInt(nums[i]);
    if (isNaN(n2)) {
        outstr += "?";
    } else if (isNallN(nums[i])) {

    } else if (n2 === 0) {
        outstr += " ";
    } else if (n2 < 1 || n2 > 26) {
        outstr += "?";
    } else {
        outstr += String.fromCharCode(n2+64);
    }
    $("textout").value = outstr;
    }
}

function isNallN(s) {
    //parse string to check all characters are digits
}

function fnEncode() {
var msg = $("textin").value.toUpperCase();
$("textin").value = msg;
if (msg === "") {
    $("textin_span").innerHTML = "* Please enter numberse to decode *";
    $("textin").focus;
    return;
} else {
    $("textin_span").innerHTML = "";
}
var c;
var outstr = "";


for (var i=0; i<msg.length; i++);
    c = msg.charCodeAt(i);
    if (typeof c === "number") {
        outstr += "99";
    }else if (c === " ") {
        outstr += 0;
    /*} else if (c[i] >= "A" && c[i] <= "Z") {
        outstr += "99";*/
    } else {
        outstr += String.charCodeAt(c - 64);
    }
    $("textout").value = outstr;
    //var x = msg.charAT(i);

}

显然isNallN还不完整,但是他向我们保证,如果我们能找到fnEncode,我们应该可以做到isNallN而不会出现任何问题(我希望这是真的,哈哈)尽管在fnEncode中做错了什么?每次运行它时,即使我输入字母,它也会给我“ 99”。

0 个答案:

没有答案