the example in this article describes a guessing game implemented in javascript. share it with everyone for your reference, the details are as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>js游戏</title> <script type="text/JavaScript"> var x=10; //x=parseInt(Math.random()*(1000-1))+1; for(var i=0;i<10;i++){ var num=prompt("请输入你猜测的数字",""); if (num!=null && num!="") { if(num==x){ switch(i){ case 0: alert('真神啦,猜对了!'); i=10; break; case 1: alert('厉害,对啦!'); i=10; break; case 2: alert('真棒,对啦!'); i=10; break; case 3: alert('好好,对啦!'); i=10; break; case 4: alert('不容易,对啦!'); i=10; break; case 5: alert('将就,对啦!'); i=10; break; case 6: alert('有点慢,对啦!'); i=10; break; case 7: alert('下次快点,对啦!'); i=10; break; case 8: alert('赶上啦,对啦!'); i=10; break; case 9: alert('最后机会,对啦!'); i=10; break; } } if(i==9){ alert('笨蛋,10次都猜不对!'); } if(num>x){ document.write("您已经猜了" + eval(i+1) + "次," + num +"," + "大了!<br>"); } if(num<x){ document.write("您已经猜了" + eval(i+1) + "次," + num +"," + "小了!<br>"); } }else{ alert('不能为空'); } } </script> </head> <body> <div id="div1"></div> </body> </html>
readers who are interested in more javascript-related content can check out the special topics on this site: "summary of javascript switching effects and techniques" "javascript search algorithm skills summary", "javascript summary of animation special effects and techniques", "summary of javascript errors and debugging techniques", "summary of javascript data structures and algorithm techniques", "summary of javascript traversal algorithms and techniques" and "summary of javascript mathematical operations usage"
i hope this article will be helpful to everyone in javascript programming.