Simple JavaScript Lucky Draw code and js Lucky Draw
Core: js Math objects and Array objects
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>random</title>
<style>
#awardListDom{width: 100%;}
</style>
</head>
<body>
<label for="awardListDom">Award List</label><br>
<input type="text" value="" id="awardListDom"> <br>
<label for="num">The prize drawn</label><br>
<input type="text" value="" id="num"> <br>
<button id="submit">Start lottery draw</button>
<script>
/*
* Idea: Random draw, one prize will be reduced by one
* Math object method: http://www.w3school.com.cn/jsref/jsref_obj_math.asp
* -random(): returns a random number between 0 and 1.
* -floor(): Get integer
* Array operation:
*-splice(x,y); x: starting position, y: get and delete the number
*/
function random(min,max){
return Math.floor(min+Math.random()*(max-min));
}
var awardListDom=document.getElementById("awardListDom"),
num=document.getElementById("num"),
submit=document.getElementById("submit");
var awardList=["First Prize","Second Prize","Second Prize","Third Prize","Third Prize","Third Prize","Encouragement Prize","Encouragement Prize", "Encouragement Award", "Encouragement Award", "Thank you for Participating", "Thank you for Participating", "Thank you for Participating", "Thank you for Participating", "Thanks for Participating", "Thank you for Participating"];
awardListDom.value=awardList;
submit.onclick=function(){
//Reference array
var oldArray=awardList;
var rNum=random(0,oldArray.length);
if(oldArray.length<1){
awardListDom.value="Activity end";
num.value="Activity end";
}
else{
num.value=oldArray[rNum];
oldArray.splice(rNum,1);
awardListDom.value=oldArray;
}
}
</script>
</body>
</html>