Skip to content

Instantly share code, notes, and snippets.

@noel9999
Created July 10, 2014 08:56
Show Gist options
  • Save noel9999/a86b973e2fd9a8a277bc to your computer and use it in GitHub Desktop.
Save noel9999/a86b973e2fd9a8a277bc to your computer and use it in GitHub Desktop.
Javascript: 取亂算數, 練習寫一支js版的猜字遊戲
//JS取亂數可以靠random()這方法,回傳是0~1以內的隨機小數
Math.random();
//設定可以取回指定範圍內亂數
function useFloor(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
}
function useCeil(min,max){
return Math.ceil(Math.random()*(max-min+1)+min-1)
}
function useRound(min,max){
return Math.round(Math.random()*(max-min)+min)
}
//隨機取陣列中的值
var myArray = new Array("A","B","C","D");
var randomGet = myArray[Math.random()*myArray.length];
//即興發揮,大家看看就好,請不要批我~!
var words = ["潤娥","孫藝珍","新垣結衣","戶田惠梨香"]
var anwser = words[Math.floor(Math.random()*words.length)];
while(reponse= prompt("請輸入 1:潤娥,2:孫藝珍,3:新垣結衣,4:戶田惠梨香 其中一人名字")){
if(reponse == anwser){
alert("Congratulation! You're a fucking genius,aren't you?");
break;
}
else{
alert("Haha, you're moron!, just take another shoot!");
}
}
alert("The anwser is: "+anwser);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment