如何在JavaScript中找到给定字符串的数值?

时间:2017-01-05 14:14:29

标签: javascript

我做了一些挖掘SO并发现了一个问题 像这样从社区中得到了很多愤怒。所以我会 只是澄清那些还没有得到它的人。

说我有一个像“Aravind”这样的字符串。数值 密码学中的那个字符串将是:
1 1 1 22 9 14 4.“ 这是因为,在“Aravind”中,第一个字母“A”是 字母表中的第一个字母,所以“A”的数值 将是1.与“R”相同,即第18个字母,因此数值 “R”是18。这一直在继续。

所以现在我正在创建一个程序,其中包括转换a 给它字符串的数值。

var latin_array = ["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"];
var integ_array = [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];

function parseToNumber (str) {
    var spl = str.split("");
    spl.forEach(function (x) {
        latin_array.forEach(function (y) {
            if (x == y) {
                // whatever i want goes here
            }
        })
    })
}

这是我已经走了多远,而且无论如何我知道我犯了一百个错误。 请原谅我,我才12岁!所以,我再次想要数值a = 1,b = 2,c = 3,d = 4种值。

提前致谢!

1 个答案:

答案 0 :(得分:2)

这样的事情会起作用吗?

function myFunction(xx, xx, numberOrigin){
    numberOrigin +=1;

    $.ajax({
    type: 'POST',
    dataType: "json",
    contentType: "application/json",
    url: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    data: JSON.stringify({
            "xxxxxxxxxxx":xxxxxxx,
            "xxxxxxxxxxxx":xxxxxxxxx,
            "xxxxxxxxxxxx":"xxxxxxx"
    }),
        success:function(output) {
            console.log(output);

            otherFunction(xxxxxxxxxxxxxxxx, numberOrigin);
       },
        error:function(output) {

       }
    });
}

function alphabetPosition(text) { var result = ""; for (var i = 0; i < text.length; i++) { var code = text.toUpperCase().charCodeAt(i) if (code > 64 && code < 91) result += (code - 64) + " "; } return result.slice(0, result.length - 1); } console.log(alphabetPosition("Aravind"));获得1 18 1 22 9 14 4。我打赌原来的Kata是一样的。