473,396 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

iterating through Array

3
Hi
I am new to all this so please excuse my ignorance. I am trying to work out how I can check if my Array contains my declared variable and if so, for the boolean to return true, however, I dont seem to be getting anywhere. I have tried the for loop and while loop as below; I would very much appreciate any help

Expand|Select|Wrap|Line Numbers
  1. var customerCodes = ['sh-12', 'gw-4', 'to-0'];
  2. var customerPasswords = ['tony', 'darma', 'checks', 'lauren'];
  3.  
  4. var password;
  5. password = window.prompt ('enter password' + '')
  6. var customerIndex;
  7. customerIndex = 0;
  8.  
  9. for (var index =0; index < customerPasswords.length; index = index + 1)
  10.  
  11. while (password != customerPasswords[customerIndex])
  12.     {
  13.  
  14.         window.prompt ('enter correct password' + '')
  15.  
  16.     }
  17.  
  18. document.write ('lala')
Feb 15 '08 #1
4 1655
mrhoo
428 256MB
some browsers implement an indexOf for array elements.
It is a handy method, you can add it if it isn't defined-
Expand|Select|Wrap|Line Numbers
  1. if(Array.prototype.indexOf== undefined){
  2.     Array.prototype.indexOf= function(what){
  3.         var L= this.length, i= 0;
  4.         while(i< L){
  5.             if(this[i]=== what) return i;
  6.             ++i;
  7.         }
  8.         return -1;
  9.     }
  10. }
Then you can see if any item is in any array-
if(customerPasswords.indexOf(password) !=-1) // password found
or
if(customerPasswords.indexOf(password) ==-1)// password not in array
Feb 15 '08 #2
joor
3
thanks for your help,

I have taken a slight different route, however, I still seem to be stuck, what I am really trying to do is to create a while loop which will keep prompting me for the password if the password does not match the strings. also, if I had a parallel array, I would like to keep the index relevant to the password position. Any ideas?? Thanks again


Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/JavaScript">
  2.  
  3.     //=================================
  4.     //    SET UP AN ARRAY
  5.     //=================================
  6.     var validate = new Array(4);
  7.  
  8.     validate[0] = "pass1"
  9.     validate[1] = "pass2"
  10.     validate[2] = "pass3"
  11.     validate[3] = "pass4"
  12.     validate[4] = "pass5"
  13.  
  14.     //=======================================================
  15.     //    FUNCTION CALLED WHEN BUTTON IS CLICKED
  16.     //=======================================================
  17. function getPassWord() {
  18.  
  19.     var getPW =document.mainForm.pw.value;    //get password from txt box
  20.     var checkPW="";
  21.     var i;
  22.     var isOK = 0;                    //SET isOK TO ZERO
  23.  
  24.  
  25.     //=====================================================
  26.     //    LOOP ROUND ALL THE VALUES IN THE ARRAY AND 
  27.     //    CHECK AGAINST ENTERED PASSWORD
  28.     //=====================================================
  29.     for (i=0; i < validate.length; i++) {
  30.  
  31.         checkPW =  validate[i];
  32.  
  33.         if (checkPW==getPW) {
  34.             isOK = 1;
  35.             break;
  36.         }
  37.     }
  38.  
  39.     //=======================================================================
  40.     //    IF isOK EQUALS 1 THEN EVERYTHING OK - ELSE PASSWORD FAILURE
  41.     //=======================================================================
  42.     if (isOK==1) {
  43.         alert("password OK = " + getPW);
  44.     }
  45.     else {
  46.         alert("password failure = " + getPW);
  47.     }
  48. }
  49.  
  50.  
  51. </script>
Feb 15 '08 #3
mars95
1
be careful!!!!

This question looks just like something in a current university assessment paper.............
Feb 17 '08 #4
acoder
16,027 Expert Mod 8TB
be careful!!!!

This question looks just like something in a current university assessment paper.............
There's a pretty strict policy on homework/classwork assignments. See the guidelines. If this can be confirmed, the code can be dealt with accordingly.
Feb 18 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Matthew Wilson | last post by:
I'm playing around with genetic algorithms and I want to write a function that mutates an integer by iterating across the bits, and about 1 in 10 times, it should switch a zero to a one, or a one...
9
by: matthurne | last post by:
I need to send just an array to a function which then needs to go through each element in the array. I tried using sizeof(array) / sizeof(array) but since the array is passed into the function,...
34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
10
by: Ken Foster | last post by:
I have a hashtable keyed by some name, holding an instance of an object. Most of the time I use the hashtable in the traditional sense, given a name, I lookup the object, then I run a method on...
3
by: pbali | last post by:
Hi, I am using PHP 5.1 and MySQL. I have a result set obtained by executing PDO:: query. I want to create an XML file by using this result set. The XML file will contain column names as XML node...
2
by: CamelR | last post by:
I have a newbie question, and I have seen reference to this mentioned in many places, but all just say "this has been discussed frequently in (other places) so we won't discuss it here..." and I am...
1
by: john | last post by:
All: I'm using MDB2 to pull data from a MySQL database... works fine, e.g.: $result = getData(...); Now that I have the $result (a multidimensional array), what is the PHP idiom for...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
0
by: SBMpy | last post by:
Instead of itereating through my half-million plus records, I'm looking for a way to query the records and return a subset of records. Q: Does dbfpy have that functionality? Q: Or is there a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.