Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts

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

    Last edited by Antoniohawk; 04-14-2003 at 02:27 AM.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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;

  • #4
    New Coder
    Join Date
    Nov 2002
    Location
    London, UK
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    like this ?

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

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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];
    
    alert( [a.search(-1),a.search(1),a.search(100),a.search(199),a.search(350)].join("\n") );
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Beautiful, just beautiful Beetle. Thx alot man, and thank u every1 else for your input.

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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;

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    10 hours later, still looking for help...

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    still needing assistance, but i prob wont get any...

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #11
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    sry man, i didn't mean to sound impatient, i was just a little bored. Take your time, and sorry again.

  • #12
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ) );
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #13
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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?

  • #14
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #15
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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 LastLast

    Posting Permissions

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