gpt4 book ai didi

Javascript:薪资计算器(无法显示正确的数字)

转载 作者:行者123 更新时间:2023-12-02 21:10:46 24 4
gpt4 key购买 nike

为了练习我的 JavaScript 技能,我参与了一个创建薪资计算器的业余项目。我的“if...else”语句遇到了问题。在我的一生中,我无法根据用户的输入获得正确的税率。无论我输入什么,控制台都显示 .10

这是 HTML:

const userSalary = document.querySelector('#salary');
const button = document.querySelector('.calculate');

if (parseInt(userSalary.value) >= 100000) {
var taxRate = 0.37;
} else {
var taxRate = 0.10;
}

button.addEventListener('click', event => {
console.log(parseInt(taxRate));
});
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title></title>
</head>

<body style="background-color: white;">
<form>
<label for="contribution">Enter contribution percentage</label>
<input type="number" id="contribution" name="contribution"><br><br>

<label for="salary">Enter your annual salary:</label>
<input type="text" id="salary" name="salary"><br><br>

<label for="tax">Select your tax filing status:</label>
<select id="tax">
<option value="single">Single</option>
<option value="married_joint">Married Filing Jointly</option>
<option value="married_separately">Married Filing Separately</option>
<option value="head_household">Head of Household</option>
</select><br><br>

<label for="paycheck_frequency">Paycheck Frequency</label>
<select id="paycheck_frequency">
<option value="weekly">Weekly</option>
<option value="biweekly">Biweekly</option>
<option value="twice_monthly">Twice a Month</option>
<option value="monthly">Monthly</option>
</select>
</form>

</body>
<button class="calculate">Calculate</button>
<script src="main.js"></script>

</html>

这是 JavaScript:

const userSalary = document.querySelector('#salary');
const button = document.querySelector('.calculate');

if (parseInt(userSalary.value) >= 100000) {
var taxRate = 0.37;
} else {
var taxRate = 0.10;
}

button.addEventListener('click', event => {
console.log(parseInt(taxRate));
});

提前谢谢您!

最佳答案

尝试使用以下 JavaScript:

const userSalary = document.querySelector('#salary');
const button = document.querySelector('.calculate');

let taxRate;

function recalculateTaxRate() {
if (parseInt(userSalary.value) >= 100000) {
taxRate = 0.37;
} else {
taxRate = 0.10;
}
}

button.addEventListener('click', event => {
// Recalculate the rate before printing it.
recalculateTaxRate();
console.log(taxRate);
});

关于Javascript:薪资计算器(无法显示正确的数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61107198/

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