Simple JavaScript Lucky Draw code and js Lucky Draw

Source: Internet
Author: User

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> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.