compare string in array

W

weetat

Hi all,

I need some advice regarding wrote a javascript function.
The function purpose is to check the variable "selectedSeat",

for e.g if the selectedSeat value is

var selectedSeat = "A:01|A:03|A:05|B:01|B:02|B:03";
var selectedArray = selectedSeat.split("|");



because row A is not in sequence , i will display alert box to
user,
but row B is in sequence is ok.

Main purpose of function is check if the row is in the sequence.
I have been cracking my head about this , anyone have suggestion is
much appreciated.

The project is in voyager browser which are the clone of netscape
4.0, so some newer javascript code will not work.

Thanks

Wee Tat
 
L

Lee

weetat said:
Hi all,

I need some advice regarding wrote a javascript function.
The function purpose is to check the variable "selectedSeat",

for e.g if the selectedSeat value is

var selectedSeat = "A:01|A:03|A:05|B:01|B:02|B:03";
var selectedArray = selectedSeat.split("|");



because row A is not in sequence , i will display alert box to
user,
but row B is in sequence is ok.

Main purpose of function is check if the row is in the sequence.
I have been cracking my head about this , anyone have suggestion is
much appreciated.

The project is in voyager browser which are the clone of netscape
4.0, so some newer javascript code will not work.

This doesn't require any special features (or even knowledge)
of Javascript. It's simply a matter of devising an algorithm.

Sort the array (if you don't know that it's already sorted)
and then step through the elements beginning with the second
one, ensuring that, if the alphabetic part is the same as
the previous element, that the numeric part is 1+ the numeric
part of that previous element.


--
 
B

Bart Van der Donck

weetat said:
The function purpose is to check the variable "selectedSeat",
for e.g if the selectedSeat value is

var selectedSeat = "A:01|A:03|A:05|B:01|B:02|B:03";
var selectedArray = selectedSeat.split("|");

because row A is not in sequence , i will display alert box
to user, but row B is in sequence is ok.

Main purpose of function is check if the row is in the
sequence.

var selectedSeat = 'A:01|A:03|A:05|B:01|B:02|B:03|C:02|D:1455'
selectedSeat += '|D:1456|ES:39|ES:38'
var selectedArray = selectedSeat.split('|')
var obj = new Object()
var chars = new Array()
var okay = new Array()

for (var i=0; i < selectedArray.length; i++) {
var seat = selectedArray.split(':')
if (chars[chars.length-1] != seat[0]) chars.push(seat[0])
if (obj[seat[0]]) {
if (!obj[seat[0]][parseFloat(seat[1]-1)]) okay.push(seat[0])
}
else {
obj[seat[0]] = new Array()
}
obj[seat[0]][parseFloat(seat[1])] = 1
}

for (var j=0; j<chars.length; j++) {
document.write(chars[j]+': ')
var neg = ''
for (var k = 0; k < okay.length; k++)
if (chars[j] == okay[k]) neg = 'no '
document.write(neg+'sequence <br>')
}

/* Notes:
1. Sequences must not start from 1 to be valid sequence
(eg. D:1455|D:1456 is okay)
2. selectedSeat must not be alphabetical, but the numbers
must be ordered per letter (eg. B:01|B:02|B:03 is okay,
B:01|B:03|B:02 is not okay)
3. Items consisting of only 1 letter (eg. C:02) are okay
*/

Hope this helps,
 
S

scripts.contact

weetat said:
Hi all,

I need some advice regarding wrote a javascript function.
The function purpose is to check the variable "selectedSeat",

for e.g if the selectedSeat value is

var selectedSeat = "A:01|A:03|A:05|B:01|B:02|B:03";
var selectedArray = selectedSeat.split("|");



because row A is not in sequence , i will display alert box to
user,
but row B is in sequence is ok.


try:

s= "A:01|A:04|A:03|B:01|B:02|B:03";
test(s)

function test(s){
var x={},ret='';
s=s.replace(/(\w):(\d+)(?=|)/g,function(a,b,c){x=(x||
[]).concat(c)});
for(var i in x)
for(var ii=1;ii<x.length;ii++)
if(x[ii]-1!= x[ii-1]){ret+=i+' - failed !!!'+"\n";break}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top