gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:19:03 27 4
gpt4 key购买 nike

就像我的其他问题一样,非常不言自明。问题是,当用户没有选择银行时,变量 bankMoney 在第一次调用 payDay 时会变为 NaN。不选择,它应该通过我的 random 函数进行随机化,但我认为不是。有人可以告诉我原因和/或修复它吗?

提前致谢。

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

理解/解决问题所需的代码片段:

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

最佳答案

从提示返回的值始终是字符串,因此请确保将它们强制转换为数字,并强制强制未定义或空值

数字 + 未定义 = NaN

关于javascript - 当我随机化银行变量时,bankMoney 变为 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27208147/

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