Help with array searching

This is a discussion on "Help with array searching" within the JavaScript Forum section. This forum, and the thread "Help with array searching are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 23rd, 2007, 07:39
New Member
Join Date: Nov 2007
Location: UK
Age: 43
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Help with array searching

Hi

This will be an east one for all you experts. I've never done scripting/programming in any form before and am driving myself mad with this one!!!!!!!

I need to search an array, which has 4 names in it, by inputting a string via window.prompt. If the name is not found I've got to ask them to re-enter their name. I'm not allowed to use a function as it's got to be straight code. I've also got another array with 4 entries and have to output the approriate message i.e. john is at names[0], therefore password is at password[0].

Hope this makes sense.

womble
Reply With Quote

  #2 (permalink)  
Old Nov 24th, 2007, 00:05
Reputable Member
Join Date: Apr 2007
Location: Scotland
Age: 17
Posts: 233
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Blake121
Re: Help with array searching

You can try adding something like this to your code, You'd have to adapt it to use window.prompt etc though.

PHP: Select all


var theName "my name"//This would be the input value from window.prompt

if(theName == names[0]) { //if theName is at names[0]

 
alert(theName " is at names[0]. Therefore password is at password[0]"); //show message of success

} else if(theName == names[1]) {//else if theName is at names[1] etc. etc.

 
alert(theName " is at names[1]. Therefore password is at password[1]");

} else if(
theName == names[2]) {

 
alert(theName " is at names[2]. Therefore password is at password[2]");

} else if(
theName == names[3]) {


 
alert(theName " is at names[3]. Therefore password is at password[3]");
} else {

 
alert(theName " has not been found. Please re-enter name");



Last edited by Blake121; Nov 24th, 2007 at 00:08.
Reply With Quote
  #3 (permalink)  
Old Nov 25th, 2007, 01:31
Reputable Member
Join Date: Dec 2005
Location: U.S.A.
Posts: 147
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via MSN to ScottR Send a message via Skype™ to ScottR
Re: Help with array searching

Sounds like you need a Multi-Dimensionan Array. Or just 2 separate arrays. One for the names, and one for the passwords. 2 separate arrays will probably be easier with JavaScript than a multi-dimensional array. Then perform a loop and when you hit a match for the name, get the password from the password array at the same index. Just make sure you keep the names and the passwords at the same index in their array.

Good Luck,
Scott
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching Blog alexgeek PHP Forum 2 Oct 27th, 2007 22:31
php searching corinne PHP Forum 6 Jul 23rd, 2006 21:44
searching help corinne Web Page Design 5 Jun 15th, 2006 11:00
Sorting a new array from an existing array Ozeona Flash & Multimedia Forum 2 Sep 20th, 2005 08:43
array unable to check another array so as to be displayed Ozeona Flash & Multimedia Forum 1 Aug 5th, 2005 10:26


All times are GMT. The time now is 05:57.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43