1

I'm trying to create a bot that interprets an array (exp table) and returns the exp difference between two levels.

I'm new to JavaScript still, so I'm sure it's something within my code, I have tried various combinations of array.find, array[x] and moving formula into variables. I just cannot seem to figure it out.

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;
    }
};`

I expect this to pull values 40 and 60 from the end of the string '!xp 40 60', interpret the array and pull the next value after 40 (3608200) and the value after 60 (29470800), then preform a calculation to take the 40 value away from the 60 value (29470800-3608200). So far I think the issue lies around the T1, T2 and T3 values as they are always returning 1, or the value in the array after 1 (300).

4
  • 1
    I would suggest you to make a 2 dimensional array out of it. [[lvl, xp], [lvl, xp], ...] would be easier to read and handle Commented Jul 10, 2019 at 10:03
  • 1
    what is message? please add the wanted result as well. Commented Jul 10, 2019 at 10:03
  • @MauriceNino thank you, that makes more sense. Commented Jul 10, 2019 at 10:06
  • @Nina Scholz this is a bot for discord, so 'message' is the string from the user when triggering the bots if and case switch. for this function of the bot, the message would be "!xp 40 60" Commented Jul 10, 2019 at 10:07

1 Answer 1

2

With one dimensional array:

You can just get the index of the lvl in the array with indexOf and then take the next value (which is the xp needed for that lvl). After that subtract the two and you are good to go:

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

With two-dimensional array (with use of a map to simplify):

Map that two dimensional array to a Map or use a Map in the first place. Then just get the correct xp value by the lvl key.

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

Sign up to request clarification or add additional context in comments.

1 Comment

one of the best replies i've ever received, thank you! theres a few new bits in here that I didn't know existed, plus answers to problems I couldnt put into words initially like the array being a list without a key. Thank you for your time, you've really helped me out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.