Javascript-问题解释数组

时间:2019-07-10 10:00:29

标签: javascript arrays

我正在尝试创建一个解释数组(exp表)并返回两个级别之间的exp差的机器人。

我还是JavaScript的新手,所以我确定这是我代码中的东西,我尝试了array.find,array [x]的各种组合以及将公式移动到变量中。我似乎无法弄清楚。

var xparray = [1, 0, 2, 300, 3, 900, 4, 2000, 5, 3700, 6, 6000, 7, 10200, 8, 16200, 9, 23550, 10, 33480, 11, 45280, 12, 60880, 13, 80480, 14, 104180, 15, 130580, 16, 161080, 17, 196480, 18, 236980, 19, 282680, 20, 333680, 21, 390280, 22, 454180, 23, 525580, 24, 604680, 25, 691780, 26, 786980, 27, 896780, 28, 1021580, 29, 1161780, 30, 1317680, 31, 1480180, 32, 1656080, 33, 1845680, 34, 2049180, 35, 2267080, 36, 2499400, 37, 2749300, 38, 3017100, 39, 3303300, 40, 3608200, 41, 3932200, 42, 4272400, 43, 4629200, 44, 5002900, 45, 5393700, 46, 5801900, 47, 6239500, 48, 6707000, 49, 7205000, 50, 7734000, 51, 8598000, 52, 9656400, 53, 10923600, 54, 12478800, 55, 14350800, 56, 16568400, 57, 19160400, 58, 22155600, 59, 25582800, 60, 29470800, 61, 33940800, 62, 38813800, 63, 44129800, 64, 49938800, 65, 56302800, 66, 63297800, 67, 71019800, 68, 79594800, 69, 89187800, 70, 100013800, 71, 112462800, 72, 126343800, 73, 141899800, 74, 159398400, 75, 179148400, 76, 201478400, 77, 226818400, 78, 255468400, 79, 288218400, 80, 325868400];


var tn = (String(message).length);
// !xp 40 60
var a = (String(message).length)-3;
var b = (String(message).length)-3;
var c = (String(message).length)-5;
var d = (String(message).length)-2;
var t0 = message.substring(b, a); //(-3,-3)
var t1 = message.substring(c, b); //(-5,-3)
var t2 = message.substring(d, tn); //(-2,0)
var na =  function(t0){return (t0*2)-1};
var nb =  function(t1){return (t1*2)-1};
var nc =  function(t2){return (t2*2)-1};
var T1 = xparray.find(function(element) {
    return element > t2});
var T2 = xparray.find(function(element) {
    return element > t0;
});
var T3 = xparray.find(function(element) {
    return element > t1;
});
var t3 = function(T1, T2, T3) {
    if (message.length = 7) {
        return T1 - T2;
    } else {
        return T1 - T3;
    }
};`

我希望它从字符串40的末尾提取值60'!xp 40 60',解释该数组,并提取40 (3608200)之后的下一个值和60 (29470800),然后执行计算以使40值与60(29470800-3608200)分开。到目前为止,我认为问题出在T1T2T3值周围,因为它们始终返回1,或者1 (300)之后的数组中的值。

1 个答案:

答案 0 :(得分:2)

具有一维数组:

您可以仅使用indexOf获取数组中 lvl 的索引,然后获取下一个值(该值是 xp 所需的< strong> lvl )。之后减去两个,您就可以开始了:

var xparray = [1, 0, 2, 300, 3, 900, 4, 2000, 5, 3700, 6, 6000, 7, 10200, 8, 16200, 9, 23550, 10, 33480, 11, 45280, 12, 60880, 13, 80480, 14, 104180, 15, 130580, 16, 161080, 17, 196480, 18, 236980, 19, 282680, 20, 333680, 21, 390280, 22, 454180, 23, 525580, 24, 604680, 25, 691780, 26, 786980, 27, 896780, 28, 1021580, 29, 1161780, 30, 1317680, 31, 1480180, 32, 1656080, 33, 1845680, 34, 2049180, 35, 2267080, 36, 2499400, 37, 2749300, 38, 3017100, 39, 3303300, 40, 3608200, 41, 3932200, 42, 4272400, 43, 4629200, 44, 5002900, 45, 5393700, 46, 5801900, 47, 6239500, 48, 6707000, 49, 7205000, 50, 7734000, 51, 8598000, 52, 9656400, 53, 10923600, 54, 12478800, 55, 14350800, 56, 16568400, 57, 19160400, 58, 22155600, 59, 25582800, 60, 29470800, 61, 33940800, 62, 38813800, 63, 44129800, 64, 49938800, 65, 56302800, 66, 63297800, 67, 71019800, 68, 79594800, 69, 89187800, 70, 100013800, 71, 112462800, 72, 126343800, 73, 141899800, 74, 159398400, 75, 179148400, 76, 201478400, 77, 226818400, 78, 255468400, 79, 288218400, 80, 325868400];

var message = '!xp 40 60';

var lvls = [parseInt(message.split(' ')[1]), parseInt(message.split(' ')[2])]; // Getting the lvls in question
lvls = lvls.map(l => xparray[xparray.indexOf(l)+1]); // Getting the xp values in question
console.log(lvls[1] - lvls[0]); // Substract xp values

具有二维数组(使用地图进行简化):

将该二维数组映射到Map或首先使用Map。然后只需通过 lvl 键获取正确的 xp 值。

var xparray = [[1, 0], [2, 300], [3, 900], [4, 2000], [5, 3700]];
var xpMap = new Map(xparray); // Map constructor takes the two dimensional array (key=lvl, value=xp)

var message = '!xp 2 3';

var lvls = [parseInt(message.split(' ')[1]), parseInt(message.split(' ')[2])]; // Getting the lvls in question

lvls = lvls.map(l => xpMap.get(l)); // Just map it by getting the value for the key

console.log(lvls[1] - lvls[0]); // Substract xp values