当我随机化银行变量时,bankMoney变为NaN

时间:2014-11-29 23:09:15

标签: javascript random var nan

像我的其他问题一样漂亮的自我解释。问题是,当此人未选择银行时,变量bankMoney在第一次调用NaN时变为payDay当他们不选择时,它应该通过我的随机化random功能,但我认为不是。有人可以告诉我为什么和/或解决它?

提前致谢。

如果需要更多代码,即:我错过的功能,缺少字符等,请告诉我。

理解/修复问题所需的代码snipets:

 function random(high,low) {
       return Math.floor((Math.random()*((high + 1) - low)) + low)
    }

function propertyIncome() {
    bankMoney = bankMoney + houseIncome
}
var job = "Part time"
    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()
    }
    var salary = 10000
    var bankMoney = 120000
    var bank = prompt("choice a bank. (Put in the number, not the name.) (You only get to do this once, so choose carefully.)     \n 1. A&A Positive income: 10% Negative income: 25%     \n2. B&C Positice income: 5% Negative income: 7%     \n3. Limburgdi International:  Positive income: 12.5% negative income: 30%     \n4.S&N Positive uncome: 2.5% Negative income: 5%")
    if(bank < 1) {
        bank = random(1,4)
    }
    if(bank > 4) {
        bank = random(1,4)
    }
    if(bank == 1) {
        function positiveIncome() {
            bankMoney = bankMoney + (bankMoney * 0.1)
        }
        function negativeIncome() {
            bankMoney = bankMoney + (bankMoney * 0.25)
        }
    }
    if(bank == 2) {
        function positiveIncome() {
            bankMoney = bankMoney + (bankMoney * 0.05)
        }
        function negativeIncome() {
            bankMoney = bankMoney + (bankMoney * 0.07)
        }
    }
    if(bank == 3) {
        function positiveIncome() {
            bankMoney = bankMoney + (bankMoney * 0.125)
        }
        function negativeIncome () {
            bankMoney = bankMoney + (bankMoney * 0.3)
        }
    }
    if(bank == 4) {
         function positiveIncome() {
            bankMoney = bankMoney + (bankMoney * 0.025)
        }
        function negativeIncome() {
            bankMoney = bankMoney + (bankMoney * 0.05)
        }
    }
    var withdrawl1 = prompt("How much would you like to withdrawl? MUST Be GREATER THAN 1 if not greater than one, it will randomize, and you may not like the outcome. \n(bank transactions are taxed 9%)")
    if(withdrawl1 < 1) {
        withdrawl1 = Math.floor((Math.random()*(bankMoney - 1)) +1)
    }
    var pocketMoney = withdrawl1 - withdrawl1 * 0.09
    bankMoney = bankMoney - withdrawl1
    var randomVariable3 = " Your pocket money is $"
    alert("Your new bank balance is $" +bankMoney +"\n" +randomVariable3 +pocketMoney)
    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)
    }
    var house = prompt("Pick a house. Put in the number, not the name. (property income is 10% with $0 tax.  \n1)Mansion: $800,000  \n2)RanchSyle: $250,000  \n3)TwoStory: $300,000  \n4)R.V.: $50,000")
    if(house == 1) {
        var houseIncome = 80000
        bankMoney = bankMoney - 800000
    }
    if(house == 2) {
       var houseIncome = 25000
       bankMoney = bankMoney - 250000
    }
    if(house == 3) {
        var houseIncome = 30000
        bankMoney = bankMoney - 300000
    }
    if(house == 4) {
        var houseIncome = 5000
        bankMoney = bankMoney - 5000
    }
    function propertyIncome() {
        bankMoney = bankMoney + houseIncome
    }
    payDay()

1 个答案:

答案 0 :(得分:1)

从提示符返回的值总是字符串,因此请确保将它们强制转换为数字,并强制使用undefined或null值

number + undefined = NaN