2015-03-08 133 views
0

我一直在工作一個小項目來學習javascript的基礎知識,但我遇到了一個我無法修復的錯誤。我做了一些研究,但無濟於事。 我想要一個產生犯罪的程序(作爲笑話)。它從數組'people'中隨機選擇一個參數,並將其與陣列'進攻'中的一個相加。一切都很順利,直到我決定讓隨機函數變成一個函數。此時它開始做奇怪的事情,比如詢問朋友的名字後停下來,將personGenerator分配給'und​​efined'。 這裏是我的代碼:如何使用參數爲變量的函數?

<script> 
    //this is plonker base 

    //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==true){ 
     //asks for another person's name 
     var person1 = prompt("Please name one of your best friends.") 
    } 

    //creates a randomizer function 
    var random = function (variable,subject){ 
     variable = subject[Math.floor(subject.length * Math.random())] 
    } 

    while(start==true){ 
     //creates array 'person' 
     var person = ["You are ","Your mum is ","Your dad is ", "The world is ", (person1 + " is ")] 
     var personGenerator 
     random(personGenerator,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 = offence[Math.floor(offence.length * Math.random())] 
     //gives out the offence 
     alert(personGenerator + offenceGenerator) 
    } 
    { 
     alert("What a plonker!") 
    } 
</script> 

我是新來的JavaScript,所以我不很瞭解。請讓你的答案容易理解。如果我在任何時候使用了錯誤的術語,請說。

感謝, 里斯C.

+0

你不需要做'if(start == true)'。你可以做'if(start)',因爲'start'是一個布爾值。 – TheDude 2015-03-08 17:12:29

+0

這段代碼有很多語法錯誤,在語句結尾處有很多缺少的分號。 – ataravati 2015-03-08 17:13:33

+0

有人告訴我,除非您使用的是舊版本的JavaScript,否則您不需要在末尾放置分號?他們做什麼,因爲我的JavaScript編碼大部分工作正常,沒有他們...... – 2015-03-08 17:17:12

回答

1

這種結構並不在Javascript工作:

//creates a randomizer function 
var random = function (variable,subject){ 
    variable = subject[Math.floor(subject.length * Math.random())] 
} 

這不會改變變量的傳遞。相反,你應該從函數中返回新的隨機值。

//creates a randomizer function 
var random = function (subject){ 
    return subject[Math.floor(subject.length * Math.random())]; 
} 

而且,然後在您使用它:

var personGenerator = random(person); 

至於爲什麼你原來的代碼不Javascript中工作,這是因爲JavaScript並沒有通過在參考有真傳你可以改變原始變量指向的方式。當你這樣做:

//creates a randomizer function 
var random = function (variable,subject){ 
    variable = subject[Math.floor(subject.length * Math.random())] 
} 

random(personGenerator, person); 

在你的隨機函數包含爲您所做的函數調用時personGenerator變量的內容的variable說法。但是,它將是一個單獨的變量。所以,這樣做:

variable = subject[Math.floor(subject.length * Math.random())] 

只改變本地函數參數的值。它不會更改personGenerator的值。

+0

工作正常!非常感謝!我花了數小時試圖弄清楚...... – 2015-03-08 17:14:37

+0

@ReeceC。 - 我在關於爲什麼原始代碼不起作用的答案的末尾添加了一些解釋。另外,你有沒有理由留下很多分號?雖然Javascript在這方面是寬容的,但它不被認爲是一種好的做法,並且在某些情況下可能導致奇怪的錯誤。 – jfriend00 2015-03-08 17:15:11

+0

感謝您的解釋 - 現在有道理!有人告訴我,分號只對老版本的js有用,現在沒有效果。如果他們確實有效果,我從來沒有注意到它。請讓我知道他們做了什麼以及如何使用它們(因爲我已經忘記如何使用它們了)。 – 2015-03-08 17:21:51

相關問題