未捕获的语法错误:未定义 x

Uncaught Syntax error: x is not defined

本文关键字:未定义 错误 语法      更新时间:2023-09-26

这里的问题是,当代码运行时,它会newJob运行函数,然后它告诉我intelligence 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();

您正在尝试使用其范围之外的变量。变量intelligence仅存在于函数colleage() 内。如果需要多个函数使用一个变量,则可以在全局空间中声明它,就像声明transactionSubVar一样。