A Little Lottery Made with JavaScript

Task

Before the Spring Festival, the company held a party with a lottery segment. The lottery consists of several rounds, each drawing a different number of people, and those who are drawn do not participate in subsequent lotteries.

Analysis

Since development was done on my own computer, but at the party site it needed to be placed on a dedicated machine connected to a projector, cross-platform compatibility was required. Employee data for the party might be adjusted frequently until the day of the event, so it was also necessary to easily interface with employee source data. Eventually, JavaScript was chosen as a purely client-side language that can run with just IE, and the display effects can be fully supported by CSS, allowing for a beautiful interface.

Environment

IE 6+

Solution

Employee data is stored separately in a text file and read using IE's Scripting.FileSystemObject;

Employee data needs to display three items: ID, name, and department name, so the text file is saved as tab-delimited data from Excel, and JavaScript is used to output it as a table; Since it's for projection display, it's all made keyboard-controlled, capturing key events with document.onkeypress; For random array sorting, JavaScript has a very convenient built-in method: array.sort(function(){return Math.random()>0.5?-1:1;});

Code

employ.txt

External data for employees are like following:

12009 name1 department1
15971 name2 department2
7815 name3 department3
9483 name4 department4
9507 name5 department5
10589 name6 department6
17212 name7 department7
15487 name8 department8
14934 name9 department9

Page

lottery.htm

```html

Lottery

Good luck for your fortune


Original link: https://www.snowpeak.fun/en/article/detail/a_little_lottery_made_with_javascript/