Question : array content validation

I have a code to fill up to 10 array positions with random numbers between 0 and 19

what I need is to check if the next random number is already present, if yes then ignore it and generate a new one, if not present then store it

thank you

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
var a = new Array(); 
var x;


for (i=0; i<=9; i++) { 
x = Math.floor(Math.random()*20);

//validation missing before insert

a[i]=x; 
} 


document.write(a[0] + "<br />"); 
document.write(a[1] + "<br />"); 
document.write(a[2] + "<br />");
document.write(a[3] + "<br />"); 
document.write(a[4] + "<br />"); 
document.write(a[5] + "<br />"); 
document.write(a[6] + "<br />"); 
document.write(a[7] + "<br />");
document.write(a[8] + "<br />");
document.write(a[9] + "<br />");

Answer : array content validation

Corrected :

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
var a = new Array(); 
	var x;
	for (i=0; i<=9; i++) { 
		x = Math.floor(Math.random()*20);
		//validation missing before insert
		a[i]=x; 
		for(var j=0;j<i;j++) {
			if(a[j]==x) {
				a.pop();
				i--;
				break;
			}
		}
	} 
	document.write(a[0] + "<br />"); 
	document.write(a[1] + "<br />"); 
	document.write(a[2] + "<br />");
	document.write(a[3] + "<br />"); 
	document.write(a[4] + "<br />"); 
	document.write(a[5] + "<br />"); 
	document.write(a[6] + "<br />"); 
	document.write(a[7] + "<br />");
	document.write(a[8] + "<br />");
	document.write(a[9] + "<br />");
Random Solutions  
 
programming4us programming4us