473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

compare string in array

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

Jun 12 '07 #1
3 1986
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.
--

Jun 12 '07 #2
weetat wrote:
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[i].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,

--
Bart

Jun 12 '07 #3

weetat wrote:
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[b]=(x[b]||
[]).concat(c)});
for(var i in x)
for(var ii=1;ii<x[i].length;ii++)
if(x[i][ii]-1!= x[i][ii-1]){ret+=i+' - failed !!!'+"\n";break}
}

Jun 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
/*-------------------------------------------------------------------------------------------------------------------------------- Parameters: $formField1: The name of the first array $formField2:...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
4
by: toadstool | last post by:
I have an array containing the various products $inventory = array(); $inventory = "whatever1"; etc I have an array containing various values $quantity = array(100,200,300); I call in a...
17
by: toadstool | last post by:
I have an array containing the various products $inventory = array(); $inventory = "whatever1"; etc I have an array containing various values $quantity = array(100,200,300); I call in a value...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.