View Full Version : Array index values
Goldilocks
02-25-2004, 05:10 AM
I have a javascript array of the alphabet as shown below. What I need to do is to this - I have two texboxes on a form. The first contains a letter of the alphabet and cannot be changed. In the second one the user needs to enter a letter of the alphabet. When they do so I need to use the array to check that the letter they have entered comes after the letter in the first box alphabetically. I thought I could use the array index values of each letter to do this but as my javascript knowledge is not that great I am having trouble figuring out how to do this.
If anyone could point in the right direction I would be very grateful! ;) Thanks.
Var alphabet = new Array(25);
alphabet[0]="A";
alphabet[1]="B";
alphabet[2]="C";
alphabet[3]="D";
alphabet[4]="E";
alphabet[5]="F";
alphabet[6]="G";
alphabet[7]="H";
alphabet[8]="I";
alphabet[9]="J";
alphabet[10]="K";
alphabet[11]="L";
alphabet[12]="M";
alphabet[13]="N";
alphabet[14]="O";
alphabet[15]="P";
alphabet[16]="Q";
alphabet[17]="R";
alphabet[18]="S";
alphabet[19]="T";
alphabet[20]="U";
alphabet[21]="V";
alphabet[22]="W";
alphabet[23]="X";
alphabet[24]="Y";
alphabet[25]="Z";
Kalessin
02-25-2004, 05:55 AM
For starters, when you declare your array, the length is 26, not 25 :D
Go to this test page (http://www.ascomi.net/kalessin/alphabetcompare.html) and view the source to see how it works. With a little tweaking it will fit in your form nicely.
Goldilocks
02-25-2004, 06:01 AM
Oops, you're right! Although I believe the 25 I put would be ignored anyway if there actually more than 25 items in the array.
Thanks for the link, I'll give it a go! ;)
Goldilocks
02-25-2004, 06:14 AM
That worked brilliantly, thanks again.
Kalessin
02-25-2004, 06:18 AM
You're welcome :)
Willy Duitt
02-25-2004, 07:05 AM
Here are two examples of making an array without actually defining an array per se. The first uses the split() method the second charAt().
<script type="text/javascript">
<!--//
function setAlpha(){
var myArr = ('a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z');
var count = Math.round((24-1) * Math.random());
var alpha = myArr.split(',');
document.forms[0].txt1.value = alpha[count];
}
function chkAlpha(){
var str = ('abcdefghijklmnopqrstuvwxyz');
var form = document.forms[0];
var txt1 = form.txt1.value.toLowerCase();
var txt2 = form.txt2.value.toLowerCase();
if(str.indexOf(txt1)<str.indexOf(txt2)){
alert('Good Job!');
form.txt2.value = '';
form.txt2.focus();
setAlpha();
}
else{
alert('Wrong!\nTry Again...');
form.txt2.value = '';
form.txt2.focus();
}
}
//-->
</script>
</HEAD>
<BODY onload="setAlpha()">
<form>
<input type="text" name="txt1">
<input type="text" name="txt2">
<input type="button" value="Submit" onclick="chkAlpha()">
</form>
I don'k know if you were trying to do something like this. But it would make a neat little game for a kidlet learning the alphabet. I think I will add a correct and wrong answer count to this before I place it in my script archive. :)
.....Willy
Willy Duitt
02-25-2004, 01:17 PM
Originally posted by UnkieB
Thanks!
Your Welcome :D
.....Willy
Jon Hanlon
02-25-2004, 05:01 PM
Goldie,
Why not just use a string compare?
if (box1.value < box2.value) ...
or
if (box2.value >= "A" && box2.value <= "Z" && box1.value < box2.value) ...
you could use value.substr(0,1) to get only the first char, but it's not necessary.
Goldilocks
02-26-2004, 03:12 AM
Ha! Sometimes the answer can be so simple that you don't see it. The reason I was thinking about using the array is because I was already using it for another function elsewhere (which did need it) and it didn't occur to me to do it the easy way! Thanks Jon.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.