subminimal
Programmer
I have a javascript that takes two text boxes, compares the numbers, and outputs the similarities to a third textbox. For example, if textbox one is 1234, and textbox two is 123, the output would be 123, as the 1,2, and 3 are the only numbers in the two that are the same. here is the code:
I need to changenge the code, so it will dynamically generate the number of boxes, for example, between 1 and 20 boxes,(as in a message will ask "how many boxes?") then the user can input numbers into those boxes and output the results the same way, only outputting the numbers that are the same in all the boxes. Is this possible? Any help would be GREATLY appreciated, as I am not very well versed in javascript. Thankyou in advance for any help you can provide.
Code:
<script language="JavaScript" type="text/javascript">
<!--
function Compare(){
var common="";
var num1=document.forms[0].num1.value;
var num2=document.forms[0].num2.value;
var res=document.forms[0].result;
var used=new Array(num2.length);
for(x=0;x<num2.length;x++){
used[x]=0;
}
for(x=0;x<num1.length;x++){
var toFind=num1.charAt(x);
for(y=0;y<num2.length;y++){
if(toFind==num2.charAt(y)){
// found digit in second string - has it been used as a match before?
if(used[y]==0){
// not matched yet... so it's a match
common+=num1.charAt(x);
used[y]=1;
y=num2.length;
} else {
// already used
}
}
}
}
res.value=common;
return false;
}
//-->
</script>
</head>
<body>
<table width="560" border="1" align="center">
<tr>
<td width="566"><form onsubmit="return Compare();">
First number:
<p>
<input name="num1" type="text">
<br>
Second number:</p>
<p>
<input name="num2" type="text">
<br>
Similarities:</p>
<p>
<input name="result" type="text">
<br>
</p>
<p>
<input name="submit" type="submit" value="Calculate">
</p>
</form>
</td>
</tr>
</table>