iterating through Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joor
    New Member
    • Feb 2008
    • 3

    iterating through Array

    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

    [CODE=javascript]var customerCodes = ['sh-12', 'gw-4', 'to-0'];
    var customerPasswor ds = ['tony', 'darma', 'checks', 'lauren'];

    var password;
    password = window.prompt ('enter password' + '')
    var customerIndex;
    customerIndex = 0;

    for (var index =0; index < customerPasswor ds.length; index = index + 1)

    while (password != customerPasswor ds[customerIndex])
    {

    window.prompt ('enter correct password' + '')

    }

    document.write ('lala')[/CODE]
    Last edited by acoder; Feb 18 '08, 10:09 AM. Reason: Added code tags
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    some browsers implement an indexOf for array elements.
    It is a handy method, you can add it if it isn't defined-
    [CODE=javascript]if(Array.protot ype.indexOf== undefined){
    Array.prototype .indexOf= function(what){
    var L= this.length, i= 0;
    while(i< L){
    if(this[i]=== what) return i;
    ++i;
    }
    return -1;
    }
    }[/CODE]

    Then you can see if any item is in any array-
    if(customerPass words.indexOf(p assword) !=-1) // password found
    or
    if(customerPass words.indexOf(p assword) ==-1)// password not in array

    Comment

    • joor
      New Member
      • Feb 2008
      • 3

      #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


      [CODE=javascript]<script language="JavaS cript" type="text/JavaScript">

      //=============== =============== ===
      // SET UP AN ARRAY
      //=============== =============== ===
      var validate = new Array(4);

      validate[0] = "pass1"
      validate[1] = "pass2"
      validate[2] = "pass3"
      validate[3] = "pass4"
      validate[4] = "pass5"

      //=============== =============== =============== ==========
      // FUNCTION CALLED WHEN BUTTON IS CLICKED
      //=============== =============== =============== ==========
      function getPassWord() {

      var getPW =document.mainF orm.pw.value; //get password from txt box
      var checkPW="";
      var i;
      var isOK = 0; //SET isOK TO ZERO


      //=============== =============== =============== ========
      // LOOP ROUND ALL THE VALUES IN THE ARRAY AND
      // CHECK AGAINST ENTERED PASSWORD
      //=============== =============== =============== ========
      for (i=0; i < validate.length ; i++) {

      checkPW = validate[i];

      if (checkPW==getPW ) {
      isOK = 1;
      break;
      }
      }

      //=============== =============== =============== =============== ===========
      // IF isOK EQUALS 1 THEN EVERYTHING OK - ELSE PASSWORD FAILURE
      //=============== =============== =============== =============== ===========
      if (isOK==1) {
      alert("password OK = " + getPW);
      }
      else {
      alert("password failure = " + getPW);
      }
      }


      </script>[/CODE]
      Last edited by acoder; Feb 16 '08, 09:33 AM. Reason: Added code tags

      Comment

      • mars95
        New Member
        • Feb 2008
        • 1

        #4
        be careful!!!!

        This question looks just like something in a current university assessment paper.......... ...

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by mars95
          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.

          Comment

          Working...