gpt4 book ai didi

javascript - 未捕获的语法错误 : x is not defined

转载 作者:行者123 更新时间:2023-12-02 16:44:27 25 4
gpt4 key购买 nike

这里的问题是,当代码运行时,它开始运行函数newJob,然后它告诉我intelligence is undefined,我将其定义为 colleageChoice == 5,或 15 之间的任何内容,但似乎没有定义它。如果任何人都可以诊断为什么它没有被定义,那将会有很大帮助。

提前致谢!

如果您需要更多信息,请告诉我!

诊断/修复问题所需的代码:

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()

最佳答案

您的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();

关于javascript - 未捕获的语法错误 : x is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27236251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com