PDA

View Full Version : Resolved RPG Level Calculator



walt jr
09-17-2012, 10:45 PM
Hello, I am attempting to make a calculator for a game called Dark Souls that will show how much souls (the currency of the game, basically) it takes to go from one level to another. I am not to worried with validation yet, so that's not the problem. Here's the code:



<html>
<head>
<title>Level Calculator</title>

<script type="text/javascript">

var levels = [0,673,690,707,724,741,758,775,793,811,829,847,1039,1238,1445,1660,1883,2114,2353,2601,
2857,3122,3396,3678,3970,4271,4581,4900,5229,5567,5915,6273,6641,7019,7407,7805,8214,
8634,9064,9505,9957,10420,10894,11379,11876,12384,12904,13436,13979,14535,15103,15683,
16275,16880,17497,18127,18770,19426,20095,20777,21472,22181,22904,23640,24390,25154,25932,
26724,27530,28351,29186,30036,30901,31780,32675,33585,34510,35450,36406,37377,38364,39367,
40386,41421,42472,43539,44623,45724,46841,47975,49126,50294,51479,52681,53901,55138,56393,
57666,58956,60265,61592,62937,64300,65682,67082,68501,69939,71396,72872,74367,75881,77415,
78869,80542,82135,83748,85381,87034,88707,90401,92115,93850,95606,97382,99180,100999,102839,
104700,106583,108487,110413,112361,114331,116323,118337,120373,122432,124514,126618,128745,
130895,133068,135264,137483,139726,141992,144282,146596,148933,151295,153681,156091,158525,
160984,163467,165975,168508,171066,173649,176257,178890,181549,184234,186944,189680,192442,
195230,198044,200884,203751,206644,209564,212511,215484,218485,221513,224568,227650,230760,
233897,237062,240255,243476,246725,250002,253307,256641,260004,263395,266815,270264,273742,
277249,280785,284351,287946,291571,295226,298910,302625,306370,310145,313950,317786,321652,
325549,329477,333436,337426,341447,345499,349583,353699,357846,362025,366236,370479,374754,
379061,383401,387773,392178,396616,401066,405590,410127,414697,419300,423937,428607,433311,
438049,442821,447627,452467,457341,462250,467194,472172,477185,482233,487316,492434,497587,
502776,508000,513260,518556,523887,529255,534659,540099,545575,551088,556637,562223,567846,
573506,579203,584937,590708,596517,602364,608248,614170,620130,626128,632164,638238,644351,
650502,656692,662921,669188,675495,681841,688226,694650,701114,707617,714180,720743,727366,
734029,740732,747476,754259,761084,767949,774855,781802,788790,795819,802889,810001,817154,
824349,831586,838864,846185,853548,860953,868400,875890,883422,890997,898615,906276,913980,
921727,929517,937351,945229,953150,961115,969124,977177,985274,993415,1001601,1009831,
1018196,1026426,1034790,1239638,1249316,1060156,1068700,1077291,1085927,1094609,1103337,
1112111,1120931,1129797,1138710,1147668,1156674,1165726,1174825,1163971,1193164,1202404,
1211691,1221026,1230408,1239838,1249316,1258841,1268415,1278037,1287707,1297425,1307192,
1317007,1326871,1336784,1346746,1356757,1366817,1376927,1387085,1397294,1407552,1417860,
1428218,1438626,1449084,1459592,1470151,1480760,1491420,1502131,1512892,1523705,1534569,
1545484,1556450,1567466,1578537,1589658,1600831,1612056,1623333,1634662,1646044,1657477,
1668964,1680503,1692095,1703740,1715438,1727189,1738993,1750851,1762762,1774727,1786746,
1798818,1810945,1823126,1835361,1847650,1859994,1872392,1884845,1897353,1909916,1922534,
1935207,1947936,1960719,1973559,1986454,1999405,2012412,2025475,2036594,2051769,2065001,
2078289,2091634,2105036,2118495,2132010,2145583,2159213,2172900,2186645,2200447,2214307,
2228225,2242201,2256235,2270327,2284478,2298665,2312954,2327280,2341665,2356109,2370612,
2385174,2399795,2414476,2429218,2444016,2458876,2473796,2488775,2503815,2518915,2534075,
2549296,2564577,2579919,2595322,2610786,2626311,2641897,2657545,2673253,2589024,2704856,
2720750,2736706,2752724,2768804,2784948,2801151,2817418,2833748,2860141,2866597,2883115,
2899697,2916342,2933060,2949622,2966657,2983556,3000519,3017546,3034637,3051792,3069012,
3086295,3103644,3121057,3138535,3156078,3173686,3191359,3209097,3226901,3244770,3262705,
3280706,3298773,3316905,3335104,3353369,3371700,3390098,3408562,3427093,3445691,3464356,
3483088,3501887,3520754,3539687,3558689,3577758,3596895,3616100,3635373,3654714,3674123,
3693601,3713147,3732762,3752446,3772199,3792020,3811911,3831871,3851900,3871999,3892167,
3912405,3932713,3953091,3973539,3994057,4014646,4035304,4056034,4076634,4087705,4118647,
4139660,4160744,4181899,4203126,4224424,4245795,4267236,4288750,4310335,4331993,4353723,
4375525,4397400,4419347,4441367,4463460,4485626,4507865,4530177,4552563,4575021,4597554,
4620160,4642840,4665594,4688422,4711324,4734300,4757351,4780476,4803677,4826951,4850301,
4873725,4897225,4920800,4944450,4968176,4991977,5015854,5039807,5063836,5087941,5112122,
5136380,5160713,5185124,5209611,5234175,5258816,5283534,5308329,5333201,5358151,5383178,
5408284,5433466,5458721,5484065,5509482,5534977,5560550,5586202,5611932,5637741,5663629,
5689596,5715642,5741767,5767972,5794255,5820619,5847062,5873585,5900188,5926871,5953634,
5980477,6007401,6034405,6061491,6088656,6115903,6143230,6170639,6198129,6225700,6253353,
6281087,6308903,6336801,6364781,6392843,6420987,6449214,6477522,6505914,6534388,6562945,
6591585,6620308,6649114,6678003,6706976,6736032,6765173,6794396,6823704,6853095,6882571,
6912131,6941775,6971504,7001317,7031215,7061198,7091266,7121419,7151657,7181981,7212389,
7242884,7273464,7304130,7334882,7365720,7396644,7427654,7458751,7489934,7521204,7552561,
7584005,7615535,7647153,7678858,7710650,7742530,7774497,7806652,7838695,7870926,7903245,
7935652,7968148,8000731,8033404,8066165,8099015,8131954,8164982,8198099,8231305,8264601,
8297986,8331461,8365026,8398682,8432426,8466260,8500184,8534200,8568306,8602502,8636790,
8671168,8705636,8740196,8774848,8809590,8844424,8879350,8914366,8949476];

function getCurrent()
{
return document.getElementById("current").value;
}

function getDesired()
{
return document.getElementById("desired").value;
}

function showSouls()
{
var current = getCurrent();
var desired = getDesired();
var needed = 0;
var i = current;

while (i < desired)
{
needed += levels[i];
i++;
}

document.getElementById("needed").value = needed;
//alert(needed);
}

</script>

</head>

<body>
<form>
<input type="text" id="current" value="current" /><br />
<input type="text" id="desired" value="desired" /><br />
<input type="button" value="Submit" onClick="showSouls();" /><br / ><br />
<input type="text" id="needed" value="" />
</form>
</body>
</html>


(Sorry for the huge array of numbers, that's just the required souls to go from one level to the next)

It works fine for the most part, but here is an example of it not working like I would think it should:
When I use the number 70 for the first text box, and then a number over 100 for the second text box, the output is simply zero, even though it should be the sum of all the numbers in the levels array from the current level to the desired level.
However, if you were to use, say, 20 for the first box and 50 for the second, it works fine.

Anybody know what I'm doing something wrong?
If you need any more explaining please ask.

traq
09-18-2012, 05:28 AM
Anybody know what I'm doing something wrong?for one:
I am not to worried with validation yet

You absolutely should be. I'll make the foolish assumption, for the moment, that you don't need to worry about security at all. But that still leaves you trying to do math on values that you don't know are actually numbers. In fact, by default, you use string values ("current" and "desired"), which gives you the result NaN.

Your current problem has to do with the same thing: you're doing math on things that aren't numbers: they're numeric strings.
If your values were numbers, then 70 < 100 would be TRUE, because seventy is less than one hundred.
However, since the values are strings, the result is FALSE, because seven is more than one.

use parseInt() to convert your strings into integers.

walt jr
09-18-2012, 05:50 AM
Thank you so much, traq! I am still pretty new to JS and had no idea it saved those variables as strings. Very good thing to know. I am liking more and more the idea of declared type variables in other languages.
- Walt Jr

traq
09-18-2012, 06:08 AM
you're welcome.