HTML Structure:
<label>Enter the Max limit random valuelabel>
<input type="text" value="" id="txtmaxvalue"/><br/>
<input type="button" value="Keep clicking me to get random values" onclick="randomise()"/>
<div id="showme">div>
<input type="text" value="" id="txtmaxvalue"/><br/>
<input type="button" value="Keep clicking me to get random values" onclick="randomise()"/>
<div id="showme">div>
Javascript code:
var rndno;
var arraylist = [];
var showme = document.getElementById("showme");
var hasvalue = false;
function randomise() {
var maxvalue = document.getElementById("txtmaxvalue").value;
findnumber(maxvalue);
showme.innerHTML = "";
for (var i = 0; i < arraylist.length; i++) {
showme.innerHTML = showme.innerHTML + arraylist[i] + ", ";
}
}
function findnumber(maxvalue) {
rndno = Math.random();
rndno = Math.floor(rndno * maxvalue);
for (var k = 0; k < arraylist.length; k++) {
if (arraylist[k] == rndno) {
hasvalue = true;
break;
}
else {
hasvalue = false;
}
}
if (!hasvalue) arraylist.push(rndno);
}
var arraylist = [];
var showme = document.getElementById("showme");
var hasvalue = false;
function randomise() {
var maxvalue = document.getElementById("txtmaxvalue").value;
findnumber(maxvalue);
showme.innerHTML = "";
for (var i = 0; i < arraylist.length; i++) {
showme.innerHTML = showme.innerHTML + arraylist[i] + ", ";
}
}
function findnumber(maxvalue) {
rndno = Math.random();
rndno = Math.floor(rndno * maxvalue);
for (var k = 0; k < arraylist.length; k++) {
if (arraylist[k] == rndno) {
hasvalue = true;
break;
}
else {
hasvalue = false;
}
}
if (!hasvalue) arraylist.push(rndno);
}
You can see the demo here
No comments:
Post a Comment