Core: Math object and Array object of js
<!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"> Awards won</label><br> <input type="text" value="" id="num"> <br> <button id="submit">Start the lottery</button> <script> /* * Idea: Random lottery, draw a prize and reduce 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: Start 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"," third prize","encouragement prize","encouragement prize","encouragement prize","encouragement prize","thank for participation","thank for participation","thank for participation","thank for participation","thank for participation","thank for participation","thank for participation"]; awardListDom.value=awardList; submit.onclick=function(){ //Reference array var oldArray=awardList; var rNum=random(0,oldArray.length); if(oldArray.length<1){ awardListDom.value="Event end"; num.value="Event end"; } else{ num.value=oldArray[rNum]; oldArray.splice(rNum,1); awardListDom.value=oldArray; } } </script></body></html>demo:http://demo.VeVB.COM/js/2015/choujiang/
github:https://github.com/litengdesign/award