


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.
