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.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can i achieve a comparison like this? 1.1 == 1.1.1 or 1.1.2 or 1.1.3

    How can i achieve a comparison like this? 1.1 == 1.1.1 or 1.1.2 or 1.1.3
    Hey guys need help on this?

    Say I have an array..
    itemNo = {1, 1.1, 1.2, 1.2.1, 1.2.2, 1.2.3, 2}

    I want to get certain group of this item numbers, say..
    x = window.prompt ("Enter parent item number");
    // I entered 1.2

    the script should then display values
    1.2.1
    1.2.2 and
    1.2.3

    take note, the elements inside the array is dynamic and
    is not ordered in any way. I mean, there is a possibility that
    the itemNumbers inside the array are to be arranged as..
    itemNo = {2, 1.2.2, 1.2.3, 1.2.1, 1.1, 1.2, 1}

    still the same requirement. help!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Just a couple of questions.

    1. If the user enters 1, will it still get 1.2.x and 1.1.x?
    2. Is there 1.10 and is it different with 1.1?
    Last edited by glenngv; 05-31-2005 at 04:55 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    May 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    Just a couple of questions.

    1. If the user enters 1, will it still get 1.2.x and 1.1.x?
    2. Is there 1.10 and is it different with 1.1?
    1. If the user enters 1, it shouldn't get neither 1.2.x nor 1.1.x. It should only get
    1.1, 1.2, 1.3, 1.4, 1.5.. etc. If he enters 1.2, he should only get 1.2.1, 1.2.2, 1.2.3, not 1.2.1.1, not 1.2.2.1..

    2. and yes. 1.10 exists and it is different from 1.1..

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    In other words, you want to get the first sublevel of a specified level.

    Try this:
    Code:
    var itemNo = ["1.1", "1.2", "1.2.1", "1.2.2", "1.2.3", "1.2.1.2", "1.10", "1.1.1", "2.1"]; //server-side generated; must be an array of string, not numbers
    function getSublevels(level){
      var len=level.length, arr=[], diff;
      for (var i=0; i<itemNo.length;i++){
        diff = itemNo[i].length-len;
        if (itemNo[i].indexOf(level)==0 && diff>0 && diff<=3){
          arr[arr.length] = itemNo[i];
        }
      }
      return arr;
    }
    
    var sublevel_1 = getSublevels("1");
    alert(sublevel_1.toString()); //["1.1", "1.2", "1.10"]
    
    var sublevel_2 = getSublevels("1.2");
    alert(sublevel_2.toString()); //["1.2.1", "1.2.2", "1.2.3"]
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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