


Implementing a simple random lottery applet based on JavaScript_javascript skills
It is more convenient to use tools such as VB, Delphi and other tools to implement small programs such as lottery. Since I do not have such an application installed on my machine, I can only find another way. In order to make the lottery program run directly on any machine without configuring the platform, the development tools and compilation and running tools can also be as simple as possible (such as text can be edited, and the browser that comes with the window system can be compiled and run) ) and decided to try using javascript. My research on JavaScript is not deep. It is mainly used in website development to judge the validity of data from the client (based on security considerations, websites with high security requirements try to use server-side languages to judge the validity of data). All involved are the most commonly used methods and functions. Therefore, the following programs can only be called relatively simple, and the interface effects and functions are very "simple".
The main key point is to get random numbers. To get random numbers within the specified range of numbers, use the formula (upper limit of range - lower limit of range + 1)*Math.random()+lower limit of range. The following is the source code:
<html> <head> <title> 抽奖小程序 </title> <!--javascript程序——Begin--> <script language="javascript"> //抽奖数字范围初始化 var from=1; //起始值 var to=1523; //终止值 numarray=new Array(); //保存抽奖数字的数组 flagarray=new Array(); //记录数字是否中奖的标示数组 countaward=new Array(0,0,0); //记录每组抽奖次数,这里只抽三组奖 /** 函数名:sysInit 传入参数:无 返回值:ture/false 功能:抽奖系统初始化,设定抽奖数字范围、初始化抽奖数字数组和标示数组 */ function sysInit() { /*设定抽奖数字范围*/ //接受用户设定操作 mixNum=prompt('起始值',1); maxNum=prompt('终止值',1523); //判断用户没有输入任何数据或输入空字符 if(mixNum!=null&&maxNum!=null&&mixNum!=""&&maxNum!="") { //判断用户输入的数据是否是合法的数字 strTemp="0123456789"; for (i=0;i<(mixNum+maxNum).length;i++) { j=strTemp.indexOf((mixNum+maxNum).charAt(i)); if (j==-1) { alert("起始数字范围不正确,程序中断!"); return false; }//end if }//end for //若用户输入合法数字,则重新设定抽奖范围 from=parseInt(mixNum); to=parseInt(maxNum); }//end if //初始化抽奖数字数组和标示数组 for(i=0;i<(to-from);i++) { numarray[i]=from+i; flagarray[i]=0; } //抽奖按钮有效 first.disabled=false; second.disabled=false; third.disabled=false; return true; } /** 函数名:getLuck 传入参数:奖次award,此项奖总数awardtotal 返回值:无 功能:无重复抽取中奖数 */ function getLuck(award,awardtotal) { var msg=""; //当抽奖数大于等于20个时,使用每次抽取10个中奖数。 for(i=0;i<(awardtotal>=20?10:1);i++) { //设定循环抽取随机数并判断,防止数字重复取 while(a=1) { //判断提示某项奖已经取完 if(countaward[award-1]==awardtotal) { alert(award+"等奖已经取满"+awardtotal+"个"); return; } //在抽奖数字范围内抽取随机数 lucky=Math.round((to-from+1)*Math.random()+from); //判断上面抽取的随机数是否已经取过 if(numarray[lucky-from]==lucky&&flagarray[lucky-from]==0) { flagarray[lucky-from]=award; countaward[award-1]++; msg+=award+"等奖N"+countaward[award-1]+":"+lucky+"\n"; break; }//end if }//end while }//end for alert(msg); return; } /** 函数名:showLuck 传入参数:无 返回值:无 功能:显示中奖的所有数 */ function showLuck() { var str1="一等奖:<p>"; var str2="二等奖:<p>"; var str3="三等奖:<p>"; for(i=0;i<(to-from);i++) { switch(flagarray[i]) { case 1: str1=str1+numarray[i]+"<br>"; break; case 2: str2=str2+numarray[i]+"<br>"; break; case 3: str3=str3+numarray[i]+"<br>"; break; } } //显示三个奖项的中奖数 document.write(str1); document.write(str2); document.write(str3); } </script> <!--javascript程序——End--> </head> <body> <center> <p><strong><font size="3" color="red">开始抽奖喽!!!</font></strong></p> <input type="button" name="range" value="设定抽奖系统" onclick="javascript:sysInit();"><p> <input type="button" name="first" value="抽取一等奖" disabled onclick="javascript:getLuck(1,3);"><p> <input type="button" name="second" value="抽取二等奖" disabled onclick="javascript:getLuck(2,20);"><p> <input type="button" name="third" value="抽取三等奖" disabled onclick="javascript:getLuck(3,100);"><p> <input type="button" name="show" value="显示抽奖结果" onclick="javascript:showLuck();"><p> </center> </body> </html>
This is the end of the simple random lottery applet code based on JavaScript. The above code comments are written in more detail. If you have any questions that you don’t understand, please feel free to ask. The editor of Script House will reply to you as soon as possible. Thank you. Everyone supports the Script House website.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.
