Join 107,218 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,843 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
I am trying to write some code to check a users reader and password in an array and display the appropriate results based on their entries. I have made 2 attempts but cannot get either to function fully. The first attempt works but when i try to write in the responses for inavlid entries it goes wrong.
CODE
Attempt 1 var readerName var readerPassword
readerName = window.prompt('Please enter your readerName','')
for ( var count = 0; count < readerNameArray.length; count++) { if(readerName == readerNameArray[count]) { readerPassword = window.prompt('please enter your password','')
i know about the semi colons i just always forget .
i created the var readerName for the user entry. So the entered readerName is checked against the array for a valid entry, is this is not needed?
I probably should clarify that the code above works fine the probems arise when i try to include the code for invalid entries. So the readerName is not found in the array i want to write out 'invalid reader Name' and then the same for the password. I have tried to enter the else statement so it would somehting like this
CODE
if(readerName == readerNameArray[count]) { readerPassword = window.prompt('please enter your password','') else { document.write('invalid readerName')
but it doesn't work!!! any ideas?
This post has been edited by truetoon: 11 Jun, 2008 - 11:18 AM
One of the problems you were having is you have a lot of extra whitespaces adn line breaks in your code. One bad thing about javascript is that it toally freeks out if there are linebreaks in function calls, outputs or arrays/variables.
Thanks for your reposnse however I am not performing a password check.
My problem I have an array of books I have an array of Due Dates as integers from 1st Jan I have an array of Status which shows which book in on loan to which reader I have an array of the book cost from which I work out a fine.
What I am tryin to do is sum up all the fines for borrowed books against a reader so I can produce something like Reader rs-14 is fined £134.00 Reader ly-14 is fined £36.24
etc My logic I am sure is wrong. I am strugling to figure out the loops I need to perform to work this out.
Thanks for your reposnse however I am not performing a password check.
My problem I have an array of books I have an array of Due Dates as integers from 1st Jan I have an array of Status which shows which book in on loan to which reader I have an array of the book cost from which I work out a fine.
What I am tryin to do is sum up all the fines for borrowed books against a reader so I can produce something like Reader rs-14 is fined £134.00 Reader ly-14 is fined £36.24
etc My logic I am sure is wrong. I am strugling to figure out the loops I need to perform to work this out.
Phak88 this is not your post yours is futher down the forum, looks like we are doing the same course though????
This is where i am up to so far, the code works apart from the code to respond to a invalid reader name.
It works through the code asking for the readername, if an invalid reade name is entered it writes out 'unknown name'. If a valid reader name is entered it asks for the password. If a valid password is entered it writes out the array but with 'Unknown name' on the last line. I have tried making the end statement an else statment but the results are even worse.
CODE
var readerNameArray = ['ly-44', 'mr-7', 'rs-14', 'al-15', 'pn-44', 'wk-46']; var readerPasswordArray = ['harding', 'grantly', 'proudie', 'slope', 'arabin', 'bold'];
var readerName var readerPassword
readerName = window.prompt('Please enter your readerName','') { for ( var count = 0; count < readerNameArray.length; count++) { if(readerName == readerNameArray[count]) { readerPassword = window.prompt('please enter your password','') {
Ah. I see the problem now. You're iterating through that array of user names, checking the name in the current spot of the array against what the user has input. Then you continue iterating to the end of array, and check it again against what the user has input. It's not working because the spot where the name is in the array won't be the same when you're at the end of the loop.
Instead, you could check the username every time you iterate one space through the loop, but if I understand what you're trying to do you won't want to do that because you'll get an error each time until you find the correct username.
What you could do is create a boolean, and once you find the correct user name set it to true. Then at the end check if that boolean is true, if it is do nothing, if it isn't output "unknown username."
Here's the code:
javascript
readerName = window.prompt('Please enter your readerName','') { var isValid = false; for ( var count = 0; count < readerNameArray.length; count++) { if(readerName == readerNameArray[count]) { isValid = true; readerPassword = window.prompt('please enter your password','') { if(readerPassword == readerPasswordArray[count]) { document.write('welcome to the catalogue'+'<BR>'+'========================'+'<BR>') for(var countA = 0; countA < bookArray.length; countA ++) document.write(bookArray[countA] + '-------'+ authorArray[countA] + '<BR>') } else {document.write('invalid password')} }
First you set the variable 'isValid' to false. When you know it is a valid username, set it to true. It will only be true if the username is valid. Then at the end check to see is 'isValid' is false. If it is, the username is invalid.
thanks Atdrago i also sorted the problem the following way
CODE
var readerPasswordArray = ['harding', 'grantly', 'proudie', 'slope', 'arabin',
'bold'];
var readerName var readerPassword
document.write('BARSETSHIRE COUNTY LIBRARIES' + '<BR>'+ '================================'+'<BR>')
readerName = window.prompt('Please enter your readerName','') { for ( var count = 0; count < readerNameArray.length; count++) { if(readerName == readerNameArray[count]) { readerPassword = window.prompt('Please enter your password','') {
thanks Atdrago i also sorted the problem the following way
CODE
var readerPasswordArray = ['harding', 'grantly', 'proudie', 'slope', 'arabin',
'bold'];
var readerName var readerPassword
document.write('BARSETSHIRE COUNTY LIBRARIES' + '<BR>'+ '================================'+'<BR>')
readerName = window.prompt('Please enter your readerName','') { for ( var count = 0; count < readerNameArray.length; count++) { if(readerName == readerNameArray[count]) { readerPassword = window.prompt('Please enter your password','') {
It seems that I have the same project, and I'm stuck too, I'm a little farther on the question. My problem is that I have my first 'if' statement working fine but the second is not responding properly. Instead of writing: no such book, it wrote book reserved+no such book
can someone please help me
Here is my coding:
var bookArray = ['Framley Parsonage 1st Ed', 'Lady, Don\'t Fall Backwards', 'How to Win Friends 2nd Ed ', 'The Death of Harry Potter', 'The Kama Sutra (unexpurgated)', 'Little Noddy Goes to the Moon']; var IndexBookArray = [ 0, 1, 2, 3, 4, 5 ]
var IndexBookArray; IndexBookArray = window.prompt('What would you like to do?' + '1. Reserve a book' + '2. Borrow a book' + ' .Enter 1 or 2 to select','')
if (IndexBookArray == 1)
IndexBookArray = window.prompt('Enter the index number of the book to be reserved','')