3 Replies - 4765 Views - Last Post: 24 June 2008 - 07:21 AM

#1 code4me   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-June 08

Help with Javascript arrays & parallel arrays

Post icon  Posted 24 June 2008 - 01:47 AM

Hi there, I urgently need to find a solution to the following problem. I need to add some scripting so the system will do the following:

Request the user's name!
If what is entered matches the data held then you are prompted to enter a password.
If this value matches as well then you receive a message saying "Welcome".
If the username is incorrect then a message appears saying "Unknown name".
If the password is incorrect then a message appears saying "Invalid password".

It has been suggested that the variable readerIndex can be used to store the index number of the logged-in reader in the three arrays of information. Negative value (e.g. -1) can represent the fact no reader has successfully logged in.

I have made a start on the script, but what is really holding me back is getting the program to cross-check the username. I would really appreciate any guidance. The script is below:

<HTML>
<HEAD>
<TITLE>Test</TITLE>

<script 

language="Javascript" 
type="text/javascript">

var readerIndex;

// Array Of Titles

var bookArray = ['Book 1', 'Book 2', 'Book 3', 'Book 4', 'Book 5', 'Book 6'];

// Array Of Authors

var authorArray = ['Author 1', 'Author 2', 'Author 3', 'Author 4', 'Author 5', 'Author 6'];

// Array Of Book Borrowing Information

var borrowedArray = ['-', '-', '-', 'rs-14', '-', '-'];

// Array Of Book Reservation Information

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

// Array Of Restrictions On Books

var restrictionArray = ['g', 'r', 'g', 'g', 'r', 'g'];

// Array Of Readers

var readerNameArray = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'];

// Array Of Reader Ages

var readerAgeArray = [21, 10, 15, 62, 41, 32];

// Array Of Reader Passwords

var readerPasswordArray = ['zzz', 'yyy', 'vvv', 'www', 'ttt', 'ppp'];


for (var count = 0; count < readerNameArray.length; count = count + 1)
window.prompt('Please enter your reader name');
{
if (count != readerNameArray[count]) 
{
document.write('<BR>' + 'Test')
document.write('<BR>' + '====')
document.write('<BR>' + 'Welcome')
}
else
{
document.write('<BR>' + 'Test')
document.write('<BR>' + '====')
document.write('<BR>' + 'Unknown name')
}
}

</SCRIPT>

</HEAD>

<BODY>
</BODY>

</HTML>



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Javascript arrays & parallel arrays

#2 truetoon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Re: Help with Javascript arrays & parallel arrays

Posted 24 June 2008 - 04:40 AM

Hi, you need another variable possibly logOnName

it would go something like this
logOnName = window.prompt etc etc



so it woul then be
if (logOnName != readerNameArray[count])
etc
etc



hope this helps a little, I have completed this same assignment so cannot give you too much (could get in trouble).

Hope you manage to get enough done before todays cut off :)


View Postcode4me, on 24 Jun, 2008 - 01:47 AM, said:

Hi there, I urgently need to find a solution to the following problem. I need to add some scripting so the system will do the following:

Request the user's name!
If what is entered matches the data held then you are prompted to enter a password.
If this value matches as well then you receive a message saying "Welcome".
If the username is incorrect then a message appears saying "Unknown name".
If the password is incorrect then a message appears saying "Invalid password".

It has been suggested that the variable readerIndex can be used to store the index number of the logged-in reader in the three arrays of information. Negative value (e.g. -1) can represent the fact no reader has successfully logged in.

I have made a start on the script, but what is really holding me back is getting the program to cross-check the username. I would really appreciate any guidance. The script is below:

<HTML>
<HEAD>
<TITLE>Test</TITLE>

<script 

language="Javascript" 
type="text/javascript">

var readerIndex;

// Array Of Titles

var bookArray = ['Book 1', 'Book 2', 'Book 3', 'Book 4', 'Book 5', 'Book 6'];

// Array Of Authors

var authorArray = ['Author 1', 'Author 2', 'Author 3', 'Author 4', 'Author 5', 'Author 6'];

// Array Of Book Borrowing Information

var borrowedArray = ['-', '-', '-', 'rs-14', '-', '-'];

// Array Of Book Reservation Information

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

// Array Of Restrictions On Books

var restrictionArray = ['g', 'r', 'g', 'g', 'r', 'g'];

// Array Of Readers

var readerNameArray = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'];

// Array Of Reader Ages

var readerAgeArray = [21, 10, 15, 62, 41, 32];

// Array Of Reader Passwords

var readerPasswordArray = ['zzz', 'yyy', 'vvv', 'www', 'ttt', 'ppp'];


for (var count = 0; count < readerNameArray.length; count = count + 1)
window.prompt('Please enter your reader name');
{
if (count != readerNameArray[count]) 
{
document.write('<BR>' + 'Test')
document.write('<BR>' + '====')
document.write('<BR>' + 'Welcome')
}
else
{
document.write('<BR>' + 'Test')
document.write('<BR>' + '====')
document.write('<BR>' + 'Unknown name')
}
}

</SCRIPT>

</HEAD>

<BODY>
</BODY>

</HTML>



** Edit ** :code:

Was This Post Helpful? 0
  • +
  • -

#3 cavvysri   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 12-June 08

Re: Help with Javascript arrays & parallel arrays

Posted 24 June 2008 - 06:27 AM

Hi truetoon, dont suppose you could give me an idea on the reserve a book question..just cant seem to get it
if (readerIndex== 1)
									{	
										{
											readerInput = window.prompt('Enter the index number of the book you wish to reserve','')
										}
										if (readerInput > 5)
												{
													document.write('No such book!');
												}
										function  from(bookStatus, from, to)
										{
 										for(count = 0; count < bookStatus.length; count++)  
										{  
											if(bookStatus[count] == 'on shelf')
											{  
												document.write('Book Reserved!');
												bookStatus[count] = 'Reserved';  
											}  
											else
											{
												document.write('You cannot reserve that book!');
											}
										}  
										return bookStatus;
										}
										from(bookStatus, 'on shelf', '');																
									}



heres the code i have, i know its completely wrong..so im just going to have to hand it in unfinished if i cant figure it out
Was This Post Helpful? 0
  • +
  • -

#4 truetoon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Re: Help with Javascript arrays & parallel arrays

Posted 24 June 2008 - 07:21 AM

View Postcavvysri, on 24 Jun, 2008 - 06:27 AM, said:

Hi truetoon, dont suppose you could give me an idea on the reserve a book question..just cant seem to get it
if (readerIndex== 1)
									{	
										{
											readerInput = window.prompt('Enter the index number of the book you wish to reserve','')
										}
										if (readerInput > 5)
												{
													document.write('No such book!');
												}
										function  from(bookStatus, from, to)
										{
 										for(count = 0; count < bookStatus.length; count++)  
										{  
											if(bookStatus[count] == 'on shelf')
											{  
												document.write('Book Reserved!');
												bookStatus[count] = 'Reserved';  
											}  
											else
											{
												document.write('You cannot reserve that book!');
											}
										}  
										return bookStatus;
										}
										from(bookStatus, 'on shelf', '');																
									}



heres the code i have, i know its completely wrong..so im just going to have to hand it in unfinished if i cant figure it out


unfortunately I also struggled on this one and the code i created only partially worked. I created a function to handle all the variations but could only get it to write out 'no such book' or 'book reserved' the other didn't work which really supprised me as the function is basically the same the one that converts the borrowed and reserved arrays into the book status.(and that worked fine) Anyhow I have already submitted the work so can only hope for the best now.

I can say that the code you have created looks really complicated I don't think it needs to be. I was really close with my effort and used only if or else statements within a function. The problems lies with calling back to the [count] within the for loop further back in the code i think(although i could be wrong). By this i mean that it needs to check the position of the readers name in the array [count] against the same position [count] of the other arrays and their respective value. This probably makes no sense but i know what i mean kinda!!!

This post has been edited by truetoon: 24 June 2008 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1