Ecuación PHP de JavaScript

I have this JavaScript equation which I'm now trying to transform to PHP.

JavaScript:

LVL=new Array(); 
LVL[1]=128; 
LVL[0]=128; 
m=.05; 
for (i=1;i<101;i++) { 
    if (i>1) { 
        LVL[i]=Math.floor(LVL[i-1]+(LVL[i-1]*m));
        m=m+.0015; 
    }
}

then it's a bunch of document.writes menos table y una for loop.

Here's what I have so far (which is NOT working):

<?php
$n = 1; // level
$m = .05; // exp modifier
$exp = floor($n*1+($n-1)*$m);
echo "Level " . $n . ", exp needed: " . $exp; // 128 exp
?>

The PHP output is: Level 1, exp needed: 1 and that's WRONG.

It DEBERÍA decir: Level 1, exp needed: 128

What am I doing wrong?

preguntado el 09 de enero de 11 a las 01:01

¿Cómo se puede controlar 1*1+(1-1)*0.05 be 128 ? You don't event have 128 in your PHP code (whereas you have it in your JS code). Your PHP code looks totally different... -

I don't need the loop because it was JUST the formula. -

2 Respuestas

You do a couple of errors:

  • you use the index (the level) as it is the amount of the experience points needed to reach the level.
  • you forgot the for (if you are testing the formula it is ok)

the code so far:

$lvl = array(128,128);
$modifier=.05; 
for ($i=1;$i<101;i++) { 
  $lvl[$i]=floor($lvl[$i-1]*(1+$modifier));
    $modifier+=0.0015; 
}
foreach ($lvl as $level=>$points) {
  echo "Level " . $level . ", exp needed: " . $points ."\n<br />"; // 128 exp
}

Respondido el 09 de enero de 11 a las 04:01

A direct transcription:

$LVL = array();
$LVL[1] = 128;
$LVL[0] = 128;
$m = .05;
for ($i = 1; $i < 101; $i++)
{
   if ($i > 1)
   {
       $LVL[$i] = floor($LVL[$i-1] + $LVL[$i-1]*$m);
       $m = $m + .0015
   }
}

You need to build the array as its built bottom-up

Respondido el 09 de enero de 11 a las 04:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.