Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2013, 12:46 AM   PM User | #1
FrostWyrmWraith
New Coder

 
Join Date: Jan 2013
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FrostWyrmWraith can only hope to improve
Coding a Forum Level Up System

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:

Code:
var plevel = Math.floor(EXP/xneeded+parseInt(1));
var xpup = (plevel*xneeded)
var showxp= (xpup-xneeded)
if(sPost.innerHTML.match('<!-- placeholder -->')) {
NewCode = sPost.innerHTML.replace("<!-- placeholder -->", ""+name + ": " + sign + EXP + "<br><!-- placeholder -->")
}
else {
NewCode =  topR + midR + name + ": " + sign +" "+ Math.floor(EXP/xneeded+parseInt(1)) + "<BR> XP: "+ (EXP-showxp) +"<BR> Experience needed: " + (xpup-EXP)+ "<BR>Overall Experience:" + (xneeded * (EXP/xneeded)) +"<br><!-- placeholder --><br><br>"
}

sPost.innerHTML = NewCode
}}
</script>
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?
FrostWyrmWraith is offline   Reply With Quote
Old 01-23-2013, 01:46 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,245
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Totally bogus:
Code:
else if (1200<EXP<1350)
correct but overkill:
Code:
else if ( 1200 < EXP && EXP < 1350 )
On top of that, the entire code makes no sense:
Code:
else if (600 < EXP && EXP < 700 ) // was: (600<EXP<700)
  {
  plevel = 2
  }
else if (650 < EXP && EXP < 750 ) // was: (650<EXP<750)
  {
  plevel = 3
  }
else if ( 800 < EXP && EXP < 900 ) // was: (800<EXP<900)
  {
  plevel = 4
  }
Ummm...and what if the value of EXP is, say 657?
The 650 in the second IF will be ignored.

OR what if the EXP is 787?
It doesn't match *ANY* of the IF's so plevel will be just blank.

Try it all over again:
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; }
... etc. ...
Think about it for a while if you don't see why that works.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 01-23-2013, 02:11 AM   PM User | #3
FrostWyrmWraith
New Coder

 
Join Date: Jan 2013
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FrostWyrmWraith can only hope to improve
Yeaaaah those nonsense ones I totally missed. I'm not sure I understand why your version works, but I'm pretty new to Javascript.

At any rate, I rewrote it based on your comments but something is still breaking it.

EDIT: Okay, it's definitely these two equations:

Code:
var xpup = (xneeded * Math.ceiling(Math.pow(1.15, plevel)))
var showxp= (xpup - (xneeded * Math.ceiling(Math.pow(1.15, plevel-1)))

Last edited by FrostWyrmWraith; 01-23-2013 at 02:51 AM.. Reason: Avoiding a double post
FrostWyrmWraith is offline   Reply With Quote
Old 01-23-2013, 03:06 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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

Last edited by xelawho; 01-23-2013 at 03:08 AM..
xelawho is offline   Reply With Quote
Old 01-23-2013, 03:25 AM   PM User | #5
FrostWyrmWraith
New Coder

 
Join Date: Jan 2013
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FrostWyrmWraith can only hope to improve
I don't know how to run the code through a browser :/

At any rate, it works now! Thanks for the help!
FrostWyrmWraith is offline   Reply With Quote
Old 01-23-2013, 04:10 AM   PM User | #6
FrostWyrmWraith
New Coder

 
Join Date: Jan 2013
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FrostWyrmWraith can only hope to improve
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:

Code:
(EXP-(xpup - (xneeded * Math.ceil(Math.pow(1.15, plevel-1)))))
Outputting like it's this:

Code:
(EXP-(xpup-xneeded))
...how does that even happen?

Last edited by FrostWyrmWraith; 01-23-2013 at 07:48 PM..
FrostWyrmWraith is offline   Reply With Quote
Old 01-23-2013, 12:56 PM   PM User | #7
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 368
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
How about you give us some actual input/output values to make sure your math is right?

Also, parseInt(1) made me laugh. Programming paranoia ... is that a thing?
Airblader is offline   Reply With Quote
Old 01-23-2013, 07:53 PM   PM User | #8
FrostWyrmWraith
New Coder

 
Join Date: Jan 2013
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FrostWyrmWraith can only hope to improve
...well at least the parseint thing wasn't me!

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.
FrostWyrmWraith is offline   Reply With Quote
Old 01-23-2013, 08:24 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,245
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-23-2013, 08:30 PM   PM User | #10
FrostWyrmWraith
New Coder

 
Join Date: Jan 2013
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FrostWyrmWraith can only hope to improve
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(/&nbsp;/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>
FrostWyrmWraith is offline   Reply With Quote
Old 01-23-2013, 08:34 PM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,245
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-23-2013, 08:44 PM   PM User | #12
FrostWyrmWraith
New Coder

 
Join Date: Jan 2013
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FrostWyrmWraith can only hope to improve
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.
FrostWyrmWraith is offline   Reply With Quote
Old 01-23-2013, 09:01 PM   PM User | #13
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 368
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
No, he just didn't see the post, I guess. But your math is still wrong.



which is neither of the values you said.
Airblader is offline   Reply With Quote
Old 01-23-2013, 09:06 PM   PM User | #14
FrostWyrmWraith
New Coder

 
Join Date: Jan 2013
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
FrostWyrmWraith can only hope to improve
Because you also did it wrong.

500*1.15^12=2700 when rounded up.

Never did I say anything about a 1.12. And where did you even get -50?

EDIT: For that matter, both values I gave were from the full equation and you just did the xpup equation minus a random 50.

Last edited by FrostWyrmWraith; 01-23-2013 at 09:11 PM..
FrostWyrmWraith is offline   Reply With Quote
Old 01-23-2013, 09:18 PM   PM User | #15
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 368
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
Quote:
Originally Posted by FrostWyrmWraith View Post
Code:
(xneeded * Math.ceil(Math.pow(1.15, plevel))-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.
Airblader is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:41 PM.


Advertisement
Log in to turn off these ads.