I'm working on modifying someone else's code for allowing forum posts to register towards a pool of experience towards leveling up. This is the original section which has been edited:
What I've done is edited plevel to be determined by an "if," "else if," "else" string and altered the equations for xpup and showxp to work with the new system. However, whenever I try to use these sections the entire code fails to function. This is the same section, edited:
Code:
if (EXP<600)
{
plevel = 1
}
else if (600<EXP<700)
{
plevel = 2
}
else if (650<EXP<750)
{
plevel = 3
}
else if (800<EXP<900)
{
plevel = 4
}
else if (900<EXP<1000)
{
plevel = 5
}
else if (1050<EXP<1200)
{
plevel = 6
}
else if (1200<EXP<1350)
{
plevel = 7
}
else if (1350<EXP<1550)
{
plevel = 8
}
else if (1550<EXP<1800)
{
plevel = 9
}
else if (1800<EXP<2050)
{
plevel = 10
}
else if (2050<EXP<2350)
{
plevel = 11
}
else if (2350<EXP<2700)
{
plevel = 12
}
else if (2700<EXP<3100)
{
plevel = 13
}
else if (3100<EXP<3550)
{
plevel = 14
}
else if (3550<EXP<4100)
{
plevel = 15
}
else if (4100<EXP<4700)
{
plevel = 16
}
else if (4700<EXP<5400)
{
plevel = 17
}
else if (5400<EXP<6200)
{
plevel = 18
}
else if (6200<EXP<7150)
{
plevel = 19
}
else if (7150<EXP<8200)
{
plevel = 20
}
else if (8200<EXP<9450)
{
plevel = 21
}
else if (9450<EXP<10850)
{
plevel = 22
}
else if (10850<EXP<12450)
{
plevel = 23
}
else if (12450<EXP<14350)
{
plevel = 24
}
else if (14350<EXP<16500)
{
plevel = 25
}
else if (16500<EXP<18950)
{
plevel = 26
}
else if (18950<EXP<21800)
{
plevel = 27
}
else if (21800<EXP<25050)
{
plevel = 28
}
else if (25050<EXP<28800)
{
plevel = 29
}
else
{
plevel = 30
}
var xpup = (xneeded * Math.ceiling(Math.pow(1.15, plevel)))
var showxp= (xpup - (xneeded * Math.ceiling(Math.pow(1.15, plevel-1)))
if(sPost.innerHTML.match('<!-- placeholder -->')) {
NewCode = sPost.innerHTML.replace("<!-- placeholder -->", ""+name + ": " + sign + EXP + "<br><!-- placeholder -->")
}
else {
NewCode = topR + midR + name + ": " + sign +" "+ if (EXP<600)
{
plevel = 1
}
else if (600<EXP<700)
{
plevel = 2
}
else if (650<EXP<750)
{
plevel = 3
}
else if (800<EXP<900)
{
plevel = 4
}
else if (900<EXP<1000)
{
plevel = 5
}
else if (1050<EXP<1200)
{
plevel = 6
}
else if (1200<EXP<1350)
{
plevel = 7
}
else if (1350<EXP<1550)
{
plevel = 8
}
else if (1550<EXP<1800)
{
plevel = 9
}
else if (1800<EXP<2050)
{
plevel = 10
}
else if (2050<EXP<2350)
{
plevel = 11
}
else (2350<EXP<2700)
{
plevel = 12
}
else if (2700<EXP<3100)
{
plevel = 13
}
else if (3100<EXP<3550)
{
plevel = 14
}
else if (3550<EXP<4100)
{
plevel = 15
}
else if (4100<EXP<4700)
{
plevel = 16
}
else if (4700<EXP<5400)
{
plevel = 17
}
else if (5400<EXP<6200)
{
plevel = 18
}
else if (6200<EXP<7150)
{
plevel = 19
}
else if (7150<EXP<8200)
{
plevel = 20
}
else if (8200<EXP<9450)
{
plevel = 21
}
else if (9450<EXP<10850)
{
plevel = 22
}
else if (10850<EXP<12450)
{
plevel = 23
}
else if (12450<EXP<14350)
{
plevel = 24
}
else if (14350<EXP<16500)
{
plevel = 25
}
else if (16500<EXP<18950)
{
plevel = 26
}
else if (18950<EXP<21800)
{
plevel = 27
}
else if (21800<EXP<25050)
{
plevel = 28
}
else if (25050<EXP<28800)
{
plevel = 29
}
else
{
plevel = 30
} + "<BR> XP: "+ (EXP-showxp) +"<BR> Experience needed: " + (xpup-EXP)+ "<BR>Overall Experience:" + (xneeded * EXP / xneeded) +"<br><!-- placeholder --><br><br>"
}
sPost.innerHTML = NewCode
}}
</script>
I am at a loss as to what I screwed up. I've tried to resolve it myself, but I haven't been able to identify exactly what the problem is other than narrowing it down to this section. Can anyone shed some light on that?
just run that code in any browser and have a look at the error console. Or open it in a decent code editor which show you the matching brackets - you'll see that this line:
Code:
var showxp= (xpup - (xneeded * Math.ceiling(Math.pow(1.15, plevel-1)))
is missing a closing bracket
and the error console will tell you that there is no such thing as Math.ceiling - you're probably looking for Math.ceil
Okay, I was wrong, something is still messed up. It's accepting the codes now, but the mathematical output is coming like I didn't change the code at all. The result for:
Code:
(xneeded * Math.ceil(Math.pow(1.15, plevel))-EXP)
Is coming out as the result would be for this:
Code:
((plevel*xneeded)-EXP)
I just...I don't even. It's doing the same for the other equation with this:
Alright, so, Let's say the person has 50 posts. That makes EXP 50 * 50 = 2500. The xneeded value is a fixed 500.
That makes the level 12. The first equation should be outputting 200 according to that. The second should be giving out 2150. Instead what I'm getting is 500 and 2000. It's basically ignoring everything I've changed, including the plevel values, and doing what would have made sense in the original code.
I think it is way way past time that you show us your complete code *AS IT IS NOW*.
And I don't understand this part, at all:
Quote:
I don't know how to run the code through a browser
If you aren't running this code in a browser, how *ARE* you running it???
Oh...and finally...
This code:
Code:
if (EXP<600) { plevel = 1; }
else if (EXP<700) { plevel = 2; }
else if (EXP<750) { plevel = 3; }
else if (EXP<900) { plevel = 4; }
else if (EXP<1000){plevel = 5; }
...
happens to be written in JavaScript, but the *logic* of it is universal. It would work the same in any computer language. Or in any human who applied the logic of it without a computer involved. So don't look at it as JavaScript code. Look at it from a logical viewpoint. Pick some EXP value (just for example, say 832, or choose one of your own) and then apply the logic/formulae there to see how it works.
You have to be able to express a problem in logical terms, in English or whatever your native language is, *FIRST*. Only then can you code it in the computer language of your choice.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
I'm putting it into the forum. It's not giving me any error codes or anything like that.
As I understood this forum's rules, I was only supposed to post the part in question. The full code:
Code:
<script type="text/javascript">
//Based on the famous money code by gornakle
//Transformed in a somewhat more automatic xp code
//by Duveaux of http://phantom-designs.net
//Do not remove credit note
//If you are using a different var for a level, you may add a sign here
var sign = "";
//You may change the name displayed here
var name = "Level";
//How much experience does a user needs to get per post?
var PerPost = 50;
//How much experience will a member start with?
var default_amount = 0;
//how much experience is needed to level up?
var xneeded= 500
//The variable UXP[uname] allows you to alter the experience for a player
//Make sure you use capitals and spell the name correct
var UXP=[];
UXP["Duveaux"]="100"
UXP["OpleX"]='0'
// No need to edit below this line!
td=document.getElementsByTagName("TD")
for(i=0;i<td.length;i++) {
if((td[i].className=="post1"||td[i].className=="post2")&&td[i].innerHTML.match("Posts: ")&&td[i].width!="100%") {
sPost=td[i].getElementsByTagName("SPAN")[0]
if(sPost.innerHTML.replace(",","").replace('.','').replace(' ','').match(/Posts: (\d+)/)) Posts = parseInt(RegExp.$1)
uname = td[i-2].innerHTML.replace(/<[^>]+>/g,"").replace(/ /g,"")
if(UXP[uname]){ EXP = (parseInt(UXP[uname]) + (Posts * PerPost))+parseInt(default_amount) }
else{ EXP = (Posts * PerPost)+parseInt(default_amount) }
var midR="Group: " + sPost.innerHTML.split('Group: ')[1]
var topR=sPost.innerHTML.split('Group: ')[0]
if (EXP<600) { plevel = 1; }
else if (EXP<700) { plevel = 2; }
else if (EXP<800) { plevel = 3; }
else if (EXP<900) { plevel = 4; }
else if (EXP<1050) { plevel = 5; }
else if (EXP<1200) { plevel = 6; }
else if (EXP<1350) { plevel = 7; }
else if (EXP<1550) { plevel = 8; }
else if (EXP<1800) { plevel = 9; }
else if (EXP<2050) { plevel = 10; }
else if (EXP<2350) { plevel = 11; }
else if (EXP<2700) { plevel = 12; }
else if (EXP<3100) { plevel = 13; }
else if (EXP<3550) { plevel = 14; }
else if (EXP<4100) { plevel = 15; }
else if (EXP<4700) { plevel = 16; }
else if (EXP<5400) { plevel = 17; }
else if (EXP<6200) { plevel = 18; }
else if (EXP<7150) { plevel = 19; }
else if (EXP<8200) { plevel = 20; }
else if (EXP<9450) { plevel = 21; }
else if (EXP<10850) { plevel = 22; }
else if (EXP<12450) { plevel = 23; }
else if (EXP<14350) { plevel = 24; }
else if (EXP<16500) { plevel = 25; }
else if (EXP<18950) { plevel = 26; }
else if (EXP<21800) { plevel = 27; }
else if (EXP<25050) { plevel = 28; }
else if (EXP<28800) { plevel = 29; }
else { plevel = 30 }
var xpup = (xneeded * Math.ceil(Math.pow(1.15, plevel)))
var showxp= (xpup - (xneeded * Math.ceil(Math.pow(1.15, plevel-1))))
NewCode = topR + midR + name + ": " + sign +" "+ plevel + "<BR> XP: "+ (EXP-showxp) +"<BR> Experience needed: " + (xpup-EXP)+ "<BR>Overall Experience:" + (xneeded * EXP / xneeded) +"<br><!-- placeholder --><br><br>"
sPost.innerHTML = NewCode
}}
</script>
But let me make a stab at this, anyway. I'll probably mess up, but w.t.h.
Okay, so you say that:
50 posts means that
EXP = 50 * 50
EXP = 2500
and, yes, I see from your (mildly messed up) table that
if ( EXP == 2500 ) { plevel = 12; }
So then let's look at your first post:
Code:
var xpup = (plevel*xneeded)
var showxp= (xpup-xneeded)
You have asserted that xneeded == 500
SO that means:
Code:
var xpup = 12 * 500 ==>> xpup should be 6000.
var showxp = 6000 - 500 ==>> showxp should be 5500.
I *really* don't see why you are then saying
Quote:
That makes the level 12. [agreed]
The first equation should be outputting 200 according to that. [I get 6000.]
The second should be giving out 2150. [I get 5500]
Care to try to explain?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
That probably has something to do with the fact that you plugged it into the original equation, not the new one. Which does result in 200 and 2150. I made a post following your previous one but it apparently needs to wait for mod approval? Anyway I posted the code and explained that it was acting as if it was reading off the original plevel formula as well, so the plevel input on those original equations isn't twelve but six, which gives you 500 and 2000. You've also missed the last half of the equation and only obtained the xpup and showxp values instead of plugging them then into the equations in I already provided, which give you what appears on the player's profile. Those subtract total EXP from xpup and showxp from total EXP.
This is the term* I am talking about. And the 1.12 is just a typo, I used 1.15 (meaning that it is in fact 2950). No random anything because your term* clearly says "-EXP".
*) As a mathematician I have a problem with calling this an equation. It simply isn't one.