I'm new to these forums and very new to JavaScript. Since my school does not offer in depth JavaScript courses, I have tried to understand a lot of it on my own.
This may be a basic question but basically I want to create 2 arrays. The first array will have a host of 'id' numbers. The second array will have price numbers. I want it so when a user enters in those id numbers in a text box it will output the values of the price array numbers in another text box.
Here's sort of an example using prompts:
noArray = new Array(3);
noArray[1]="03";
noArray[2]="12";
noArray[3]="15";
nameArray = new Array(3);
nameArray[1] = "$45";
nameArray[2] = "$300";
nameArray[3] = "$900";
var userNumIn=prompt("Enter the item number you want to retrieve","");
var itemSub=1;
var matchInd= false;
while (itemSub <= 3 && matchInd == false){
if (noArray[itemSub] == userNumIn){
matchInd = true;
} else {
itemSub++ ;
}
}
if (matchInd == true){
document.write("The item costs " + nameArray[itemSub]);
} else {
document.write("Invalid item number");
}
The question is how do I compare the value of what someone typed in the an "id" textbox with the id array and then if that value matches, have it check the price array for the price of that id and then output to a 'price' textbox? I hope you understand what I mean.
document.write()facility is kind-of a bad habit to get into.noArraylooks like[ , "03", "12", "15"](and similarly fornameArray): you should be assigning tonoArray[0],noArray[1],noArray[2]. And also, Javascript hastrueandfalse, which you should use instead of"Y"and"N".