views:

210

answers:

5

I have the following function:

function getLevel(points)
{
  var level = -1 + Math.sqrt(4 + points/20);

  // Round down to nearest level
  return Math.floor(level);
}

The above function calculates the level of a player based on their points, my problem is that I need a function like this to calculate the points needed for a given level.

+12  A: 

The inverse of that function would be:

points = ((level + 1)**2 - 4) * 20

(where ** is the power operator).

Greg Hewgill
sorry, but this is not working well...
coolboycsaba
for level 10 it should give 2340 , this gives 3960
coolboycsaba
What's not working? What language are you using?
nevets1219
`((10+1)**2-4)*20 = 2340`, so this is correct for that instance (and in general)
BlueRaja - Danny Pflughoeft
His answer is correct - you've incorrectly applied it most likely.
pdehaan
+6  A: 
level = -1 + (4 + points / 20) ** 0.5
level + 1 = (4 + points / 20) ** 0.5
(level + 1) ** 2 = 4 + points / 20
(level + 1) ** 2 - 4 = points / 20
20 * ((level + 1) ** 2 - 4) = points
zildjohn01
+4  A: 
Math.sqrt(4 + points/20) =        level + 1

          4 + points/20  =       (level + 1)^2

              points/20  =       (level + 1)^2 - 4

              points     = 20 * ((level + 1)^2 - 4)
                         = 20 * ((level^2 +  2*level + 1) - 4)
                         = 20 * ( level^2 +  2*level - 3     )
                         = 20 *   level^2 + 40*level - 60
Eric
thanks, this one worked : points = 20*level * level + 40*level - 60;
coolboycsaba
They should all work. Just remember that in javascript, the `^` operator does *not* mean "raise to the power of".
Eric
+2  A: 

20(L+1)2 - 80

BlueRaja - Danny Pflughoeft
+2  A: 

Should be fairly easy, just solve for points:

               level              = -1 + Math.sqrt(4 + points / 20)
               level + 1          =      Math.sqrt(4 + points / 20)
      Math.pow(level + 1, 2)      =                4 + points / 20
      Math.pow(level + 1, 2) - 4  =                    points / 20
20 * (Math.pow(level + 1, 2) - 4) =                    points

So:

points = 20 * (Math.pow(level + 1, 2) - 4)
Lasse V. Karlsen