...

View Full Version : Searching An Array



Antoniohawk
04-14-2003, 02:12 AM
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;
...





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++;
}

x_goose_x
04-14-2003, 04:09 AM
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

Antoniohawk
04-16-2003, 09:36 PM
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;

Adam20002
04-16-2003, 11:12 PM
like this ?




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

beetle
04-16-2003, 11:32 PM
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];

alert( [a.search(-1),a.search(1),a.search(100),a.search(199),a.search(350)].join("\n") );

Antoniohawk
04-18-2003, 03:42 AM
Beautiful, just beautiful Beetle. Thx alot man, and thank u every1 else for your input.

Antoniohawk
04-18-2003, 09:12 PM
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;

Antoniohawk
04-19-2003, 04:25 AM
10 hours later, still looking for help...

Antoniohawk
04-19-2003, 09:56 PM
still needing assistance, but i prob wont get any...

beetle
04-20-2003, 06:25 AM
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.

Antoniohawk
04-20-2003, 04:39 PM
sry man, i didn't mean to sound impatient, i was just a little bored. Take your time, and sorry again.

beetle
04-22-2003, 04:29 PM
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 ) );

Antoniohawk
04-23-2003, 02:43 AM
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?

beetle
04-23-2003, 03:02 AM
Look at the parameters

expStart
How much experience to start with

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.

Antoniohawk
04-23-2003, 03:23 AM
maybe this will be easier for me if i show u the page

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

beetle
04-23-2003, 03:34 AM
whoop, sorry. Tiny typo

while ( tempExp >= levels[++i] ) {}

Antoniohawk
04-23-2003, 04:07 AM
thats prob it, brinkster is down tho, ill try it tomm morn

Antoniohawk
04-24-2003, 02:27 AM
actually the reason i am confused with that 25 is that i dont think i explained it clearly. The 25 should belong like this

tempExp += level * 1.75 + 25

but there is no need for sending a value of 25 to the function, because 25 will always be 25 because that is the equation. Though i fixed that part, i doesnt seem to be giving me the correct answer, the answer seems to be way too high. For example i entered in

alert(findEventQty( 8000156, 8001156));

and received 23, when in fact the real answer is 5. No idea wats happening, maybe u do.

beetle
04-25-2003, 01:31 AM
That's because you are still using my dummy array

function findEventQty( expStart, expEnd )
{
// removed
var tempExp = expStart;
var counter = 0;
var level = getLevel();

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

function getLevel()
{
var i = -1;
while ( tempExp >= lvl[++i] ) {}
return i;
}
}
And, according to the function AND my own calculation, the answer is 6 :D

Antoniohawk
04-25-2003, 09:12 PM
lmao, well i guess im not in my right mind lately. Thx alot beetle, I havent tested it yet, but i bet its wat im looking for. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum