Javascript simple Lucky Draw code _ javascript tips-js tutorial

Source: Internet
Author: User
At the end of the year, various companies had lottery activities. I also wrote a super simple code to achieve the lucky draw effect. There was no gorgeous Css3 effect, but there was a short and concise js Code. Core: js Math objects and Array objects

 
  Random  Award List

Prize received

Start drawingScript/** idea: random lottery, draw an award to reduce a * Math object method: http://www.w3school.com.cn/jsref/jsref_obj_math.asp *-random (): Return 0 ~ A random number between 1. *-Floor (): returns an integer * Array Operation: *-splice (x, y); x: Start position, y: Number of retrieved and deleted */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", "Encouragement Award ", "Encouragement Award", "Thank you for participating", "Thank you 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 ended"; num. value = "activity ended";} else {num. value = oldArray [rNum]; oldArray. splice (rNum, 1); awardListDom. value = oldArray;} script

Demo: http://demo.jb51.net/js/2015/choujiang/

Github: https://github.com/litengdesign/award

Related Article

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.