...

View Full Version : How can i achieve a comparison like this? 1.1 == 1.1.1 or 1.1.2 or 1.1.3



Det7
05-31-2005, 03:41 AM
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!

glenngv
05-31-2005, 04:52 AM
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?

Det7
05-31-2005, 06:57 AM
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..

glenngv
05-31-2005, 07:49 AM
In other words, you want to get the first sublevel of a specified level.

Try this:

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"]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum