...

View Full Version : processing parallel arrays



kelbly
06-17-2008, 04:37 PM
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.



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

Kor
06-17-2008, 05:21 PM
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.


var shelfArray = new Array();
for(var index = 0; index < bookArray.length; index++){
if(borrowedArray[index] == '-'){
shelfArray[shelfArray.length] = 'onshelf';
}
else{
shelfArray[shelfArray.length] = 'borrowed';
}
}

kelbly
06-17-2008, 07:26 PM
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

Kor
06-17-2008, 08:28 PM
Well, we all have learned basically by errors.:)

Was my advice of any help? Have you solved the problem?

kelbly
06-17-2008, 08:48 PM
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

rnd me
06-18-2008, 10:33 AM
Processing Parallel Arrays


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 }

kelbly
06-18-2008, 01:55 PM
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.


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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum