2015-03-08 16 views
-3

我一直在做一些基本的js,但我不確定我是否正確使用分號。 這裏是我的代碼: 我在js中使用分號嗎?

//creates a variable that will start the game 
var start = confirm("Are you sure want to participate in plonker base alpha?"); 

//starts and loops the game 
if(start){ 
    //asks for another person's name 
    var person1 = prompt("Please name one of your best friends.") 
    } 

//creates a randomizer function 
var random = function (subject){ 
    return subject[Math.floor(subject.length * Math.random())] 
} 
while(start){ 
    //creates array 'person' 
    var person = ["You are ","Your mum is ","Your dad is ", "The world is ",(person1 + " is ")]; 
    var personGenerator = random(person); 

    //creates an array 'offence' 
var offence = ["an idiot!", 
"a complete pysco!!!", 
"a smelly, worthless peice of junk!", 
"a whale re-incarnated that looks like a squirrel!", 
"a dumb pile of dirt that has the misfortune of seeing itself in the mirror once in a while!", 
"a complete and utter plonker!", 
"a dumbo!", 
"a right dufus!!!", 
"a pile of rabbit dung!", 
"an intelligant, good looking king being... Did I mention - it's opposite day!", 
"a bum-faced rat!!!", 
"a fat, lazy oaf!", 
"a blobfish look-alike!!!!!", 
"a lump of toenail jelly!"]; 
var offenceGenerator = random(offence); 

//gives out the offence 
alert(personGenerator + offenceGenerator); 
} 
{ 
    alert("What a plonker!") 
    } 

如果我使用他們錯了請糾正我的意見。

感謝, 里斯C.

+1

主要問題似乎是縮進,而不是分號......無論如何,你可能想問一下[codereview.se]。 – JJJ 2015-03-08 22:15:28

+2

如果您使用的是「錯誤」,那意味着您會在某個地方出現語法錯誤。你做? – usr2564301 2015-03-08 22:15:45

+1

每個語句都應該以分號結束,但是你通常可以避開它,因爲瀏覽器可以很好地猜測語句在哪裏結束。通過http://jslint.com運行你的代碼,這將給出一個錯誤,警告和不良實踐的列表。 – 2015-03-08 22:17:11

回答

0

修改以下行,它會尋找對我好,半colonwise。

var person1 = prompt("Please name one of your best friends."); 
return subject[Math.floor(subject.length * Math.random())]; 
alert("What a plonker!"); 

分號僅需要在JavaScript當兩個語句是在同一行中,像這樣:

i=0;j++ 

因此,可以愉快地省略分號時語句由換行分離,如:

i=0 
j++ 

然而,結束用分號每個語句可以被認爲是更嚴格的方法(這樣所有的語句都會以同樣的方式結束),並且可以幫助您在稍後避免神祕的錯誤。

可以找到更多的信息here,herehere。另請參閱this SO問題。

+0

感謝您的所有細節!這有意義在最後!我會從現在開始正確地做... – 2015-03-09 17:33:46

+0

不客氣,@ReeceC。隨時歡迎你回答你的問題,幫助你解答任何問題。如果您的問題已得到解答,您也可以通過點擊旁邊的灰色複選標記來接受其中一個答案作爲「最佳答案」。 – Richard 2015-03-09 18:04:59

0

u需要在以下幾行

VAR PERSON1 =提示( 「請列出你最好的朋友之一。」)

迴歸主題[Math.floor(subject.length加分號*的Math.random ())]

警報( 「什麼plonker!」)

希望它幫助

歡呼

+0

謝謝!我讀過分號不能用於花括號,所以我猜這意味着你仍然在一個大括號內使用它們,但不是在一個之後?請告訴我。 – 2015-03-09 17:32:44

+0

這就是糾正你的腳本不會工作,如果你嘗試關閉你的if和其他條件或自制函數用分號,但在變量的末尾和被稱爲函數,爲你做一些事情的例子: var variable = alert(「給我一個警報「); <= promt(「提示我一些東西」); – 2015-03-10 11:38:53

1

在JavaScipt中,分號(;)用於表明您完成了語句。簡單地轉向新的生產線也是一樣。例如:

// These two things are the same 
foo1(); 
foo2(); 

foo1() 
foo2() 

// Note you can do this with semi-colons 
foo1(); foo2(); 
// But you can't do this 
foo1() foo2() 

你的代碼很好,但是有些地方很糟糕,例如直接在循環或if語句之後放分號。

// The semi-colon ends the if statement, so the alert runs. 
if(false); 
    alert("Hello Wolrd") 

但大多數情況下,分號是可選的,您可以根據需要使用它們。儘管人們傾向於更多地使用它們。因爲對於許多其他語言,它們是必需的。

+0

好的,謝謝!將來我會記住這一點。 :-) – 2015-03-09 17:31:29

相關問題