Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Javascript arrays-help!


  • Please log in to reply

#1
rps2

rps2

    New Member

  • Member
  • Pip
  • 3 posts
I'm trying to write a piece of code so I can check a username array for a username_input. I know I have to loop through each item in the array to see if it matches the username_input but I just can't get it to work!

I know I need to:

for each array item in username array:
- store the array index in a variable
- see if that array value matches the user input

but how do I put this into code?


please can someone help, it's sendind me insane! :)
  • 0

Advertisements


#2
jpshortstuff

jpshortstuff

    Member

  • Member
  • PipPipPip
  • 119 posts
http://www.w3schools...loop_for_in.asp

Something like this:
var x;
var index;

for (x in username_array)
{
	 if (username_array[x] == username_input){
		   //do whatever you want to do if theres a match e.g.
		  index = x;
	 }
}

Hope that helps, any more questions just ask.
  • 0

#3
rps2

rps2

    New Member

  • Topic Starter
  • Member
  • Pip
  • 3 posts
Thanks so much for the reply I have managed to check the username_input with the username_array and then stored the index number of the currently logged in user as var index but how do I add code so that if the username_input isn't in the array it displays as 'unknown name' ?

Edited by rps2, 30 May 2008 - 12:20 PM.

  • 0

#4
jpshortstuff

jpshortstuff

    Member

  • Member
  • PipPipPip
  • 119 posts

Thankyou so much for your reply it really did help me.

But now I have another question - sorry.
So I am trying to write code to enable me to search an array for a username and if it is there to store the index number which I have written like this from your help:

var userName_Array = ['a','b','c','d','e','f'];

var userName_input = window.prompt('Please enter your username,'');

for (x in userName_Array)

if ( userName_Array[x] == useName_input )
{
readerIndex = x
document.write('thankyou' + '<BR>');
}

I want it to display a 'Sorry unknown user name' if the name is not in the array, but when I add code to this it indeed displays 'Sorry unknown user name'
but it also displays this when the userName_input is in the array, for every element in the array that the userName_input doesn't match.

How do I check the array as a whole, so if the userName_input is not equal to any of the elements in the array - display 'Sorry unknown user name'

but also stop it displaying 'Sorry unknown user name' until it reaches a match with one of the elements.

So if I was to put username as 'e' in the prompt box, the output displays 'Sorry unknown user name' five times then it displays 'thankyou' as a match for index number 4, then displays 'Sorry unknown user name' for the last element that it doesn't match!
How do i stop it from doing this?


Please post what code you have currently, then I can see what needs to be done.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP