0

while the user is still entering the values of this array

for( var i=0;i<names.length;i++)
{  
   ID[i]=prompt("Enter The Id number of "+names[i],"ID number");
   for(var w=0;w<ID.length;w++)
{ 
   if(ID[i]==ID[i-1])
   {
       window.alert("You Have Already Entered That Id Number!");
       break;
   }
}

}

i know this only compares the ID[1]with ID[0] for ex.

i don't want it to compare ID[0] because its the first element entered

so how can i modify it in order to compare ID[i] with all the previously entered ID s?

  • looks like the problem is in your if block change it like this and it should work ID[w]==ID[i]. and of course you don t want to compare some item so add w != i && ID[w]==ID[i]. – Onur Topal Nov 07 '12 at 15:41
  • 1
    Check this solution: http://stackoverflow.com/questions/237104/array-containsobj-in-javascript – Adam Bubela Nov 07 '12 at 15:44

1 Answers1

0

This will allow you to re-enter duplicates values.

for( var i=0;i<names.length;i++)
{
   ID[i]=prompt("Enter The Id number of "+names[i],"ID number");
   if(ID.slice(0,i).indexOf(ID[i])>-1)
   {
      window.alert("You Have Already Entered That Id Number!");
      i--;
   }
}

edit - apparently Internet Explorer <= 8 doesn't implement Array.indexOf() so if that's an issue you'd need to add it yourself.

Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52