2014-12-01 54 views
0

這裏的問題是,代碼運行時,它就會在運行功能newJob,然後它告訴我intelligenceundefined,我把它定義wheather colleageChoice == 5,或15之間的任何東西,但似乎沒有定義它。如果任何人都可以診斷爲什麼它沒有被定義,這將有極大的幫助。未捕獲的語法錯誤:X是沒有定義

提前致謝!

如果您需要更多信息,請告訴我!診斷/修復問題所需

代碼:

var transactionSubVar = 0 
function transaction() { 
    transactionSubVar = prompt("Here's a bank transaction. Put in a positive number to deposit, or a negative to withdrawl. if your deposit will give you a negative pocketMoney, the bank won't allow it.") 
    transactionSubVar = parsefloat(transactionSubVar) 
    if((pocketMoney - transactionSubVar) < 0) { 
     confirm("The bank won't allow this transaction, sorry.") 
    } 
    if((pocketMoney - transactionSubVar) > 0) { 
     bankMoney = bankMoney + (transactionSubVar - (transactionSubVar * 0.09)) 
     pocketMoney = pocketMoney - (transactionSubVar - (transactionSubVar * 0.09)) 
    } 
    round() 
    alert("Your bank Money is now $" +bankMoney +randomVariable3 +pocketMoney) 
} 
function parsefloat(number) { 
    var numberNoCommas = number.replace(/,/g, ""); 
    return parseFloat(numberNoCommas); 
} 
function random(high,low) { 
    return Math.floor((Math.random()*((high + 1) - low)) + low) 
} 
var job = "Unemployed" 
function round() { 
    bankMoney = Math.ceil(bankMoney * 100)/100; 
    pocketMoney = Math.ceil(pocketMoney * 100)/100; 
} 
function intrest() { 
    if(bankMoney > 0) { 
     positiveIncome(0) 
    } 
    if(bankMoney < 0) { 
     negativeIncome(0) 
    } 
    round() 
} 
function payDay() { 
    bankMoney = bankMoney + (salary - (salary * 0.05)) 
    intrest() 
    propertyIncome() 
    round() 
    alert("You have been paid,and your bank account has been given intrest. your new bank balance is: $" +bankMoney) 
} 
payDay() 
transaction() 
function colleage() { 
    var currentColleagePrice = random(300000,75000) 
    alert("The current average price for colleage is: $" +currentColleagePrice +" Keep this in mind when choosing a colleage.") 
    var colleage1Price = random(10000,100) + currentColleagePrice 
    var colleage2Price = currentColleagePrice - random(5000,-1000) 
    var colleage3Price = random(25000,1000) + currentColleagePrice 
    var colleage4Price = random(10000,-2000) + currentColleagePrice 
    var colleageChoice = prompt("Choose a colleage. (As always, put in the number, not the name.) \n (Remember, the average price currently is: $" +currentColleagePrice +"/n1) $" +colleage1Price +"\n 2) $" +colleage2Price +"\n 3) $" +colleage3Price +"\n 4) $" +colleage4Price +"\n 5) No colleage") 
    colleageChoice = parseFloat(colleageChoice) 
    if(colleageChoice == 1) { 
     bankMoney = bankMoney - colleage1Price 
     var colleage = true 
    } 
    else if(colleageChoice == 2) { 
     bankMoney = bankMoney - colleage2Price 
     var colleage = true 
    } 
    else if(colleageChoice == 3) { 
     bankMoney = bankMoney - colleage3Price 
     var colleage = true 
    } 
    else if(colleageChoice == 4) { 
     bankMoney = bankMoney - colleage4Price 
     var colleage = true 
    } 
    else { 
     var colleage = false 
    } 
    if(colleage == true) { 
     alert("Welcome to colleage " +colleageChoice +"!. For coming here, you have earned 5,000 intelligence. This can be used to earn better jobs later on.") 
     var intelligence = 5000 
    } 
    if(colleage == false) { 
     alert("We're sorry you choose not to go to colleage, but for your troubles a team of people taught you some basic job skills. You earned 500 intelligence from them. Intelligence can be used to earn better jobs later on.") 
     var intelligence = 500 
    } 
} 
colleage() 
transaction() 
function newJob() { 
    var newjob = "" 
    alert("Choose a job. (You can always put in 0 to keep your current one.)\n(Remember to put in the number instead of the job title.)") 
    if(intelligence > 25000) { 
     newjob = prompt("0: "+job +"salary: " +salary +"\n1: Hosiptial doctor. \n2: Vetrenarian \n3: Dentist ") 
     newjob = parsefloat(newjob) 
     if(newjob > 3) { 
      newjob = random(1,3) 
     } 
     if(newjob < 0) { 
      newjob = random(1,3) 
     } 
     if(newjob == 1) { 
      salary = 2500 
      job = "Hospital doctor" 
     } 
     if(newjob == 2) { 
      salary = 2000 
      job = "Vetrenarian" 
     } 
     if(newjob == 3) { 
      salary = 3000 
      job = "Dentist" 
     } 
    } 
    if(25000 > intelligence > 10000) { 
     newjob = prompt("0: "+job +"salary: " +salary +"\n1: Engineer \n2: Nurse \n3: Accountant") 
     newjob = parsefloat(newjob) 
     if(newjob > 3) { 
      newjob = random(1,3) 
     } 
     if(newjob < 0) { 
      newjob = random(1,3) 
     } 
     if(newjob == 1) { 
      salary = 1750 
      job = "Engineer" 
     } 
     if(newjob == 2) { 
      salary = 1500 
      job = "Nurse" 
     } 
     if(newjob == 3) { 
      salary = 1250 
      job = "Accountant" 
     } 
    } 
    if(intelligence < 10000) { 
     newjob = prompt("0: "+job +"salary: " +salary +"\n1: Waitor/waitress \n2: Janitor \n3: Radio station operator") 
     newjob = parsefloat(newjob) 
     if(newjob > 3) { 
      newjob = random(1,3) 
     } 
     if(newjob < 0) { 
      newjob = random(1,3) 
     } 
     if(newjob == 1) { 
      salary = 750 
      job = "Waitor/Waitress" 
     } 
     if(newjob == 2) { 
      salary = 200 
      job = "Janitor" 
     } 
     if(newjob == 3) { 
      salary = 550 
      job = "Raido talk host" 
     } 
    } 
} 
newJob() 
payDay() 
+3

智力超出範圍。在函數之外定義它來引用其他函數中的值。 – kingdamian42 2014-12-01 20:02:10

+1

或者更好的是,擁有'colleage'功能(不應該拼寫'college'?)* return *'intelligence'的值。然後將該行更改爲'var intelligence = colleage()' – 2014-12-01 20:03:04

+0

將智能定義爲以下任何函數:var intelligence = 0;然後設置它沒有var – bksi 2014-12-01 20:04:02

回答

2

var intelligence只是裏面的方法colleage()的範圍。如果你想引用其他方法var,那麼你需要創建intelligence外:

var intelligence; 
function colleage() { 
    ... 
    if(colleage == true) { 
     alert("Welcome to colleage " +colleageChoice +"!. For coming here, you have earned 5,000 intelligence. This can be used to earn better jobs later on."); 
     intelligence = 5000; 
    } 
    if(colleage == false) { 
     alert("We're sorry you choose not to go to colleage, but for your troubles a team of people taught you some basic job skills. You earned 500 intelligence from them. Intelligence can be used to earn better jobs later on."); 
     intelligence = 500; 
    } 
} 

您也可以返回變量:

function colleage() { 
    ... 
    if(colleage == true) { 
     alert("Welcome to colleage " +colleageChoice +"!. For coming here, you have earned 5,000 intelligence. This can be used to earn better jobs later on."); 
     return 5000; 
    } 
    if(colleage == false) { 
     alert("We're sorry you choose not to go to colleage, but for your troubles a team of people taught you some basic job skills. You earned 500 intelligence from them. Intelligence can be used to earn better jobs later on."); 
     return 500; 
    } 
    //it shouldn't reach here but make sure to handle this case if it does 
} 
var intelligence = colleage(); 
0

您正在嘗試利用外部的變量其範圍。變量intelligence只存在於您的函數colleage()中。如果您需要一個變量供多個函數使用,您可以在全局空間中聲明它,就像您聲明transactionSubVar一樣。

+1

我覺得真的很愚蠢期待一個變量中聲明的函數作爲一個全局函數。雖然謝謝! – Travis 2014-12-01 20:07:14

相關問題