Created
March 31, 2017 08:26
-
-
Save gt11799/46816b745b4755921fd998ace9e429e6 to your computer and use it in GitHub Desktop.
js practise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <body onload="guess_game();"> | |
| <script> | |
| var count = 0; | |
| function guess_game() { | |
| var colors = ['black', 'red','maroom', 'yellow','olive','lime','green']; | |
| colors.sort() | |
| var should_choose = Math.floor(Math.random() * colors.length); | |
| var answer = ''; | |
| while (true) { | |
| count++; | |
| answer = prompt('在这几种颜色中选择一个颜色: ' + colors.join(',')); | |
| index = colors.indexOf(answer); | |
| if (index == -1) { | |
| alert('你选择的颜色不在选项内'); | |
| } else if (index == should_choose) { | |
| alert('恭喜你,猜对了!一共用了' + count + '次'); | |
| document.body.style.background = colors[should_choose]; | |
| break; | |
| } else if (index < should_choose) { | |
| alert('猜错了,答案比你填的要大'); | |
| } else { | |
| alert('猜错了, 答案比你填的要小'); | |
| } | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment