为什么我的bankMoney值变为NaN而不是数字

时间:2014-11-29 02:52:13

标签: javascript function variables var

标题解释了这一切。当我运行代码时,在第139行,bankMoney值为NaN而不是数字。我很确定问题出在我定义一个改变值的值的函数中。这些可能是payDayintrestpropertyIncome

如果有人可以帮助我,我会非常感激,因为我一整天都在使用这段代码,而这一个错误阻止了我向前编码。

我知道我在随机点中定义了很多变量,如果有人决定用机器来解决这个问题,或者手动,我会很高兴,如果我可以使用编辑过的代码,但这不是必需的,因为它不是'我的问题。

代码是:

var intrest = function(intrest) {
    if(bankMoney > 0) {
        positiveIncome(0)
    }
    if(bankMoney < 0) {
        negativeIncome(0)
    }
}
var age = prompt("How old do you want your character to be? (Will be randomized if not between 20 and 100.)")
if(age > 100) {
    age = Math.floor((Math.random()*81) +20)
}
else if(age < 20) {
    age = Math.floor((Math.random()*81) +20)
}
var randomVariable1 = " Years old"
var randomVariable2 = " Press 'OK' if you like this age, or 'Cancel' to have it randomized."
var ageConfirm = confirm("Your character is now " +age +randomVariable1 +randomVariable2)
if(ageConfirm === true) {}
else {
    age = Math.floor((Math.random()*81) +20)
}
confirm("Your character is now " +age +randomVariable1)
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.     1. A&A Positive income: 10% Negative income: 25%     2. B&C Positice income: 5% Negative income: 7%     3. Limburgdi International Positive income: 12.5% negative income: 30%     4.S&N Positive uncome: 2.5% Negative income: 5%")
if(bank < 1) {
    Math.floor((Math.random()*4)+1)
}
if(bank > 4) {
    Math.floor((Math.random()*4)+1)
}
if(bank === 1) {
    function positiveIncome(positiveincome) {
        bankMoney = bankMoney + (bankMoney * 0.1)
    }
    function negativeIncome(negativeincome) {
        bankMoney = bankMoney + (bankMoney * 0.25)
    }
}
if(bank === 2) {
    function positiveIncome(positiveincome) {
        bankMoney = bankMoney + (bankMoney * 0.05)
    }
    function negativeIncome(negativeincome) {
        bankMoney = bankMoney + (bankMoney * 0.07)
    }
}
if(bank === 3) {
    function positiveIncome(positiveincome) {
        bankMoney = bankMoney + (bankMoney * 0.125)
    }
    function negativeIncome (negativeincome) {
        bankMoney = bankMoney + (bankMoney * 0.3)
    }
}
if(bank === 4) {
     function positiveIncome(positiveincome) {
        bankMoney = bankMoney + (bankMoney * 0.025)
    }
    function negativeIncome(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.")
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 $"
confirm("Your new bank balance is $" +bankMoney +randomVariable3 +pocketMoney)
var jumped = Math.floor(Math.random()*75000)
if(jumped > 50000) {
    confirm("You escaped being jumped by four guys. A bounty hunter gave you $250 for leading the criminals to him.")
    pocketMoney = pocketMoney + 250
}
else {
     var jumpedChoice = confirm("You were jumped by four guys, they took half of your pocketmoney! Press okay to continue about your day, or cancel to chace them down.")
     if(jumpedChoice === true) {
        pocketMoney = pocketMoney - pocketMoney * 0.5
    }
    else {
        var trip = Math.floor(Math.random()*100000)
        if(trip < 75000) {
            confirm("You tripped and fell, and they came back and took the rest of your money.")
            pocketMoney = 0
        }
        else {
        confirm("You found them, and they were cornered between you and the police. They were forced to give you your money back.")
        }
    }
}
var fired = Math.floor(Math.random()*75000)
if(fired > 70000) {
    var newJob = prompt("You lost your job. Choose a new job. Put in the number, not the job title. 1)Cashier  2)HairStylist  3)Driver  4)Janitor  5)Landscaper")
    if(newJob === 1) {
        salary = 500
    }
    if(newJob === 2) {
        salary === 250
    }
    if(newJob === 3) {
        salary = 750
    }
    if(newJob === 4) {
        salary = 200
    }
    if(newJob === 5) {
        salary = 1000
    }
}
function payDay(payDay) {
    bankMoney = bankMoney + (salary - (salary * 0.05))
    intrest(0)
    propertyIncome(0)
    confirm("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.  1)Mansion:$800,000  2)RanchSyle:$250,000  3)TwoStory:$300,000  4)House:$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(propertyIncome) {
    bankMoney = bankMoney + houseIncome
}
payDay(0)
var transactionSubVar = 0
function transaction(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.")
    if((transactionSubVar - pocketMoney) < 0) {
        confirm("The bank won't allow this transaction, sorry.")
    }
    if((transactionSubVar - pocketMoney) > 0) {
        bankMoney = bankMoney + transactionSubVar
        pocketMoney = pocketMoney - transactionSubVar
    }
}
transaction(0)
confirm("Your bank Money is now $" +bankMoney +randomVariable3 +pocketMoney)

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用parseFloat()函数http://www.w3schools.com/jsref/jsref_parsefloat.asp

另外,我注意到你将许多变量传递给未使用的函数。例如

function propertyIncome(propertyIncome) {
    bankMoney = bankMoney + houseIncome
}

不使用传递给它的(propertyIncome)。

答案 1 :(得分:0)

在评估用户的住宅选择时,您使用strict equality===)将其输入与各种值进行比较:

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
}

由于prompt返回一个字符串,所有这些比较都会返回false,而houseIncome仍为undefined。将undefined添加到bankMoney最终会产生NaN

您可以使用==代替===

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
}

或者在使用严格相等之前使用parseInt

house = parseInt(house, 10);

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
}

示例: http://jsfiddle.net/9tfgcr5p/