compare string in array

Discussion in 'Javascript' started by weetat, Jun 12, 2007.

  1. weetat

    weetat Guest

    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
    weetat, Jun 12, 2007
    #1
    1. Advertising

  2. weetat

    Lee Guest

    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.


    --
    Lee, Jun 12, 2007
    #2
    1. Advertising

  3. 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.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
    Bart Van der Donck, Jun 12, 2007
    #3
  4. 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=(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}
    }
    scripts.contact, Jun 14, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,781
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  2. Replies:
    10
    Views:
    772
  3. Replies:
    10
    Views:
    748
    Roland Pibinger
    Jan 24, 2007
  4. Davy

    Can I compare array with array?

    Davy, Jul 16, 2006, in forum: Perl Misc
    Replies:
    12
    Views:
    147
    Charles DeRykus
    Jul 17, 2006
  5. Replies:
    2
    Views:
    412
Loading...

Share This Page