Travis
Travis

Reputation: 1284

Uncaught Syntax error: x is not defined

The problem here is that when the code runs, it gets to running the function newJob, then it tells me intelligence is undefined, I define it wheather colleageChoice == 5, or anything between 1 and 5, but it seems not to be defining it. If anyone could diagnose why it isn't being defined it would help tremendously.

Thanks in advance!

Please tell me if you need more info!

Code needed to diagnose/fix problem:

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

Upvotes: 0

Views: 511

Answers (2)

pglynn
pglynn

Reputation: 200

You are trying to utilize a variable outside of its scope. The variable intelligence only lives inside of your function colleage(). If you need a variable to be used by more than one function, you can declare it in global space, just like you have declared transactionSubVar.

Upvotes: 0

You var intelligence is only inside the scope of the method colleage(). If you want to reference the var in the other methods, then you need to create intelligence outside:

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;
    }
}

You can also return the variable:

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

Upvotes: 2

Related Questions