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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    processing parallel arrays

    Processing Parallel Arrays

    --------------------------------------------------------------------------------

    Hey,
    just wondering if anyone can offer advice as to what i have so far?

    I havent put the whole of my script here, just the little bit that I am stuck on.


    Im trying to get the status of the book to show whether the book is borrowed, reserved or on the shelf.


    Code:
    if (readerPassword == readerPasswordArray[count])
                {
                    document.write('<BR>'+'<BR>'+'Welcome to the catalog'+'<br>'+'================='+' <BR>' )
     
     
                    for (var index = 0; index < bookArray.length; index++)
                    var shelfArray = new Array
                    (if (borrowedArray[index] == ('-'))
                    {
                    shelfArray[index] = ('onshelf')
                    }
                    else
                    {
                    if (borrowedArray[index] != ('-'))
                    {
                    shelfArray[index] = ('borrowed')
                    }
                    })
     
     
                    document.write ( index + ' --- ' +bookArray[index] + '---------' + authorArray[index] +' ----------'  + shelfArray[index] +'<br>'  );
                    '<br>'
                    document.write ('What would you like to do?'+ '<br>' + '<br>')
                    document.write('1. Reserve a Book'+ '<br>')
                    document.write('2. Borrow a Book'+ '<br>')
    var borrowedArray = ['-', '-', '-', 'rs-14', '-', '-'];

    // Array Of Book Reservation Information

    var reservedArray = ['-', 'ly-44', '-', '-', '-', '-'];

    what I want to happen is where the borrowed array or reserved array have a '-' i want the document to write on shelf, and where there is a reader name eg. 'ly-44' at the index 1, the words resered or borrow to be written. I have tried if statements galore, but to no avail.


    I hope I have made some sense?

    I hope that it is possible to do this
    Kind regards

    kelbly

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Declare the array outside the loop, otherwise it will be initialized each time. And take care about the syntax: for(){}, Array(), , else{}, else if{}, don't nest the strings inside parenthesis... and so on.
    Code:
     
    var shelfArray = new Array();
    	for(var index = 0; index < bookArray.length; index++){
    		if(borrowedArray[index] == '-'){
    		shelfArray[shelfArray.length] = 'onshelf';
    		}
    		else{
    		shelfArray[shelfArray.length] = 'borrowed';
    		}
    	}
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    Thank you for your kind response, ive been all a bit sloppy with the coding because of I have literally spent a week trying to do it!! just trying trial and error now lol!!

    Kelbly

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Well, we all have learned basically by errors.

    Was my advice of any help? Have you solved the problem?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Well I havent solved it, I did as you suggested and the list is coming back as undefined. What I have done is turn the coding which you helped with in to a status () function then tried to call it up so it in line with the other books, autors etc to show the status. but as I said it comes back as undefined.

    Was I right in using it as a function? as I was a bit unsure where to put it!! i know where I would like to put it, but thats another story lol!

    heres an example of what I am getting

    Welcome to the catalog
    =================
    0 --- Framley Parsonage 1st Ed---------Trollope, A ----------undefined
    1 --- Lady, Don't Fall Backwards---------Dagger, D ----------undefined
    2 --- How to Win Friends 2nd Ed ---------Carnegie, D ----------undefined

    where it says undefined I want it to have the status

    I hope that makes sense

    Katie

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,346
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by kelbly View Post
    Processing Parallel Arrays

    Code:
    if (readerPassword == readerPasswordArray[count])
                {
                    document.write('<BR>'+'<BR>'+'Welcome to the catalog'+'<br>'+'================='+' <BR>' )
     
     
                    for (var index = 0; index < bookArray.length; index++)
                    var shelfArray = new Array
                    (if (borrowedArray[index] == ('-'))
                    {
                    shelfArray[index] = ('onshelf')
                    }
                    else
                    {
                    if (borrowedArray[index] != ('-'))
                    {
                    shelfArray[index] = ('borrowed')
                    }
                    })
     
     
                    document.write ( index + ' --- ' +bookArray[index] + '---------' + authorArray[index] +' ----------'  + shelfArray[index] +'<br>'  );
                    '<br>'
                    document.write ('What would you like to do?'+ '<br>' + '<br>')
                    document.write('1. Reserve a Book'+ '<br>')
                    document.write('2. Borrow a Book'+ '<br>')
    var borrowedArray = ['-', '-', '-', 'rs-14', '-', '-'];

    // Array Of Book Reservation Information

    var reservedArray = ['-', 'ly-44', '-', '-', '-', '-'];

    [/CODE]
    kelbly

    should be }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Re: Processing Parallel Arrays

    --------------------------------------------------------------------------------

    Right so you helped me get that sorted, I now have made myself another headache! once the catalogue has been printed out the user has the choice to borrow or reserve a book.

    if the user enters a choice where the book has already been borrowed or reserved they have to be told so. other wise they are told that they have borrowed or reserved a book.

    Code:
    document.write ('What would you like to do?'+ '<br>' + '<br>');
    document.write('1. Reserve a Book'+ '<br>');
    document.write('2. Borrow a Book'+ '<br>');
     
     
     var choice;
     var reserve;
     choice = window.prompt('please type option 1 or 2')
      if (choice == '1')
      {
      reserve = window.prompt('please type the index of the book to reserve')
        for ( var index = 0; index < shelfArray.length; index++) {
       var reserveArray = new Array;
       if ((shelfArray[index] == 'onshelf') && (shelfArray[index] != 'reserved') || (shelfArray[index] == 'borrowed'))
       {
       reserveArray[index] = ('your book has been reserved');
       }
       else
       {
       if (shelfArray[index] != 'onshelf')
       {
       reserveArray[index] = ('you cannor do this')
       }
       }
     
     
      document.write(reserveArray[index])
     
      } 
     
      }
    this is my code I have so far, but im just not getting anywhere fast!

    you cannor do thisyou cannor do thisyou cannor do thisyou cannor do thisyou cannor do thisyour book has been reserved

    this is the output I get!!

    any pointers?


  •  

    Posting Permissions

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