Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Searching An Array

I have a number, stored as a variable, numero1. I want to search my array of numbers to find the position that number falls in. For example, if the number is >= the number at lvl[2] and < lvl[3] then i want to return the number 2. I hope u can understand. That was just an example, but the array consists of 100 spots and i will prob have to search them all.

...
lvl[2]=83;
lvl[3]=174;
...

Edit:

After I thought about it, I decided I might have the answer. If not, this is kinda wat im looking for. Check it for me plz.

var num2;
var i2 = 1;
for (i = 0; i <= myArray.length; i++)
{
if (num1 >= lvl[i] && num1 < lvl[i2])
{
num2 = i;
i = myArray.length;
}

i2 = i++;
}

• Are the numbers in order?
If so, use the following:

for ( x=1; x<myArray.length ) {
if ( num1 >= lvl[x] ) break;
}

// x will equal the point where it broke the loop

• that looks really easy, and as i examined it a realized that you were close but not quite there. Lets say we have the number 100. We start at lvl[1], which is 0. Then it checks to see if 100 (num1) is greater then or equal to 0. It appears that it is so the loop stops. Look at the example of an array below. num1 is 150 and we run thru a loop. At the end of the loop, I want a variable to be equal to 2, because 150 is between lvl[2] and lvl[3], and I want the lower number to be returned. Maybe im not explaining this well enuf, I have no idea. If you have any questions please ask.

lvl[0] = 0;
lvl[1] = 0;
lvl[2] = 100;
lvl[3] = 200;
lvl[4] = 300;

• like this ?

Code:
for ( x=1; x<myArray.length ) {
if ( num1 >= lvl[x] && num1 < lvl[x+1] ) break;
}

• Code:
Array.prototype.search = function( val )
{
if ( val < this[0] ) return -1;
var l = this.length - 1;
for ( var i = 0; i < l; i++ )
{
if ( this[i] <= val && this[i+1] > val )
return i
}
return l;
}

var a = [0,0,100,200,300];

• Beautiful, just beautiful Beetle. Thx alot man, and thank u every1 else for your input.

• I was wondering if we could take this a little further. Below is wat i want to do:

We start with having a certain amount of experience as in a number, level 1 being equal to 0 experience, because you just started (notice the array below). Now we are going to light a fire and we get 26.75 experience for making one fire ((level * 1.75) + 25). We now have 26.75 exp. and want to keep making fires. We eventually make enough fires to have 83 or more exp. and now are gonna get a little bit more exp for making one fire, because we are now at level 2. We keep going and come to lvl 3 and so forth. Wat i want to do is calculate how many fires we need to make, starting at a number of exp. and ending at another number of exp., taking all of the above into account. Man that was hard to explain and i hope that some1 can understand it. If you have any questions at all please ask.

var lvl = new Array(11)
lvl[0]=0;
lvl[1]=0;
lvl[2]=83;
lvl[3]=174;
lvl[4]=276;
lvl[5]=388;
lvl[6]=512;
lvl[7]=650;
lvl[8]=801;
lvl[9]=969;
lvl[10]=1154;

• 10 hours later, still looking for help...

• still needing assistance, but i prob wont get any...

• Dude. Calm down. It's Easter weekend. People busy spending time with their families - as I am.

Besides, outside of the context of this forum you'd be paying somebody like me for this work.

Patience.

• sry man, i didn't mean to sound impatient, i was just a little bored. Take your time, and sorry again.

• Code:
function findEventQty( expStart, expEnd, eventValue )
{
var levels  = [-1,0,83,174,276,388,512,650,801,969,1154];
var tempExp = expStart;
var counter = 0;
var level   = getLevel();

while ( tempExp < expEnd )
{
tempExp += level * 1.75 + eventValue;
level    = getLevel();
counter++;
}
return counter;

function getLevel()
{
var i = -1;
while ( tempExp <= levels[++i] ) {}
return i;
}
}

alert( findEventQty( 0, 500, 25 ) );

• alert( findEventQty( 0, 500, 25 ) );

the 25 in the above, is that the experience that you receive to start, at the level u are at at that moment?

• Look at the parameters

expStart

expEnd
Experience value to stop at (as you put it "and ending at another number of exp.")

eventValue
How much the experience-earning event is worth. In this case, it was the fire example you gave, which was 25.

• maybe this will be easier for me if i show u the page

http://www34.brinkster.com/antoniohawk/help.html

•
Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•