Jump to content

Restricting the last iteration in the loop


NICON

Recommended Posts

So I have a very simple for loop that I am populating a list with levels and exp required to achieve the next level. I have been using a sandbox to test my output and have also created an excel sheet to replicate the data to verify the content. This code was not mine from the beginning and in creating the spreadsheet I discovered the flaw. Below is the code I am trying to fix:

function experience($L, $pres = 0) {
    $a = 0;
    $end = 0;
	for ($x = 1; $x < $L; ++$x) {
        $a += $x * round($x + 5 * pow(4, ($x / 300)));
	}
	if ($x > 199) {
		$a += $x * round($x + 7 * pow(4, ($x / 290)));
	}
	if ($x > 399) {
		$a += $x * round($x + 11 * pow(4, ($x / 280)));
	}
	if ($x > 599) {
		$a += $x * round($x + 19 * pow(4, ($x / 270)));
	}
	if ($x > 799) {
		$a += $x * round($x + 35 * pow(4, ($x / 260)));
	}
	if ($x > 999) {
		$a += $x * round($x + 67 * pow(4, ($x / 250)));
	}
	return round($a / 1.25);
}

Below is the troubleshooting I am attempting to do (Modified and simplified for and while loop):

//for loop
$no = 200;
$a = 0;
for ($x = 1; $x < $no; ++$x) {
    $a += $x + 1;
}
if ($no > 199) {
    $a += $x + 2;
}
echo $a;


//while loop
$no = 200;
$a = 0;
$x = 1;
while (($x - 1) < $no) {
    $a += $x * round($x + 5 * pow(4, ($x / 300)));
    $x++;
    if ($x > 199) {
        $a += $x * round($x + 7 * pow(4, ($x / 290)));
    }
}
echo $a;

Upon request I can also provide snipets of the excel sheet. So the gist of what the issue I am having is this. Between level 199 and 200 the iteration of the loop is running one extra time through the initial formula. As you can tell at level 200, 400, 600, 800 and 1000 I want this formula to change so that it simplifies the amount of exp required to achieve the next level. I can not for the life of me figure out how to restrict the loop while still allowing the values 200 up to run through it for the first 199 iterations.

The output I should get from the primary code for levels 199 through 201 are as follows: 199 = image.png.15722c92671d8ecb9e0125de3b96b140.png 200 = image.png.f57027b6ce07a1395664210684487621.png 201 = image.png.786a96bca6e4c1b0f882f267fe667a77.png

At level 199 I am good but for whatever reason it runs one additional iteration at the level 1-199 formula and then moves on to the 200 level formula messing up the values.

Any and all help is much appreciated. I am a novice hobbyist at best and have been doing this for many, many years now but sometimes I get stumped. I chalk it up to lack of proper training and time to really be serious with it.

Thanks in advance....NICON

Link to comment
Share on other sites

All of your if statements are outside of your for loop, meaning they will only run once after your loop completes.  I think maybe you want them inside the for loop.  Making that change makes the result for 200 match what you posted.  Adding a clause to only run the first calculation when x is less than 200 fixes the 201 result.

Also, the way your if statements are structured if $x is say 650 you will run each of the formula up to that, ie:

$a += $x * round($x + 7 * pow(4, ($x / 290)));
$a += $x * round($x + 11 * pow(4, ($x / 280)));
$a += $x * round($x + 19 * pow(4, ($x / 270)));

When I am guessing you only want to run the last one.  If that is correct, then you want to change your if statements to be a chain of if..elseif..else statements that check the ranges using < rather than >, ie

if ($x < 200){
	...
} else if ($x < 400){
	... 
} else if ($x < 600){
	...
} ...

 

Link to comment
Share on other sites

This is what I come up with base on your recomendation. However the numbers still get lost after 200:

<?php
    $no = 201;
    $a = 0;
	for ($x = 1; $x < $no; ++$x) {
	    if ($no < 201) {
	       $a += $x * round($x + 5 * pow(4, ($x / 300)));
	    } elseif ($no < 401) {
	        $a += $x * round($x + 7 * pow(4, ($x / 290)));
	    } elseif ($no < 601) {
	        $a += $x * round($x + 11 * pow(4, ($x / 280)));
	    } elseif ($no < 801) {
	        $a += $x * round($x + 19 * pow(4, ($x / 270)));
	    } elseif ($no < 1001) {
	        $a += $x * round($x + 35 * pow(4, ($x / 260)));
	    } else {
	        $a += $x * round($x + 67 * pow(4, ($x / 250)));
	    }
        echo round($a / 1.25);
	}
?>

Below are the values for 199 through 210. Running the above gets me 2367891 vs 2302679. Something is odd here and why its running an extra time through the previous condition is perplexing to me. All the values are identical up to and including 200 using this code.

image.png.305bd12010e66fe867c17ec1f98d3602.png

Link to comment
Share on other sites

Ignore the above I missed the variable being carried through. $x vs $no... Below is what it should have been. Thanks so much i think this got it working correctly.

<?php
    $no = 400;
    $a = 0;
	for ($x = 1; $x < $no; ++$x) {
	    if ($x < 200) {
	       $a += $x * round($x + 5 * pow(4, ($x / 300)));
	    } elseif ($x < 400) {
	        $a += $x * round($x + 7 * pow(4, ($x / 290)));
	    } elseif ($x < 600) {
	        $a += $x * round($x + 11 * pow(4, ($x / 280)));
	    } elseif ($x < 800) {
	        $a += $x * round($x + 19 * pow(4, ($x / 270)));
	    } elseif ($x < 1000) {
	        $a += $x * round($x + 35 * pow(4, ($x / 260)));
	    } else {
	        $a += $x * round($x + 67 * pow(4, ($x / 250)));
	    }
        echo round($a / 1.25);
	}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.