Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

one variable different values 1

Status
Not open for further replies.

kurayami

Technical User
Mar 12, 2008
31
i have this script and I'm trying to make it work:

<head>
function go (){
var x, j, k;

x = document.getElementById('code').value;
j = ['1', '2', '3', '7', '8', '10'];
k = [ '4', '5', '6', '9'];

if (x == j) { document.getElementById('final').value = "A+"; }
if (x == k) { document.getElementById('final').value = "A-"; }

}

</head>

<input type = "text" id = "code" maxlength = 2 size = 2 />
<input type = "text" id = "final" maxlength = 2 size = 2 readonly = "readonly" />
<input type = "button" id = "code" maxlength = 2 size = 2 value = "go" onClick = "go();" />


What I am trying to do is that if user enters a number like 1-3,7-8, or 10 the 'final' field will show A+. If 4-6 or 9 then 'final' field will show A-. I cannot seem to get this to work if this is the script. I have tried using if..else and it does work, however, the data that I am working on has about 100+ codes.

"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
-Douglas Adams
 
You can't directly ciompare an array like that. You have to look into its internal values for comparison.

You can use the indexOf function to find a specific value in the array. So in your case you wan't to check if its not there. indexOf retunrs a -1 if it can't find the value in the array.

Code:
if (j.indexOf(x)!=-1) {  document.getElementById('final').value = "A+"; }
else if (k.indexOf(x)!=-1) {  document.getElementById('final').value = "A-"; }

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks, vacunita! That had solved my problem although the script do not like the bracket, comma and apostrophe so I changed it instead to like this:

function go (){
var x, j, k;

x = document.getElementById('code').value;
j = "1 2 3 7 8 10";
k = "4 5 6 9";

if (j.indexOf(x) != '-1') { document.getElementById('final').value = "A+"; }

else if (k.indexOf(x) != '-1') { document.getElementById('final').value = "A-"; }
}
}
</head>

<input type = "text" id = "code" maxlength = 2 size = 2 />
<input type = "text" id = "final" maxlength = 2 size = 2 readonly = "readonly" />
<input type = "button" id = "code" maxlength = 2 size = 2 value = "go" onClick = "go();" />

This works like it should be. A star for you.

"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
-Douglas Adams
 
Hi

Just two things :
[ul]
[li][tt]Array.indexOf()[/tt] returns an integer, if you compare it with a string [tt][green]'-1'[/green][/tt] you force the interpreter to make a pointless cast. Better keep it as numeric [tt][teal]-[/teal][purple]1[/purple][/tt], as Phil wrote it.[/li]
[li]The [tt]id[/tt]s must be unique per document. Having multiple elements with the same [tt]id[/tt] ( code in your case ) can cause weird behavior in some browsers.[/li]
[/ul]


Feherke.
 
I agree on both counts with feherke.

By the way, It should work with the array as you had it. What you are doing now is using a string instead of the array you had previously. Whatever error you were getting was surely due to something else.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top