gpt4 book ai didi

javascript - 我的计算在 jscript 中不起作用

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

我对 javascript 编码非常陌生,并且我对构造函数/参数有足够的了解,但不足以进行故障排除。

这是我的有问题的代码部分。我试图从一系列计算中产生一个四舍五入的整数值,但我似乎无法让它工作。

如果工作正常,它将获取 20000 值,将其乘以 3 [60000],然后除以 3250 [18.4615...],然后向下舍入到最接近的数字 [18]。然后它将浏览器中的文本重写为 18。

$(document).ready(function() {
var moneyInSeats = $('#moneyInSeats');
var moneyInSeatsValue = moneyInSeats;
var moneyMultiplier = 3;

if (moneyInSeatsValue.text() != '') {
var seatsVisible = ((moneyInSeatsValue.text()) * moneyMultiplier);
var seatsPerSection = 3250;
var sectionsVisibleExact = seatsVisible / seatsPerSection;
var sectionsVisibleRounded = Math.floor(sectionsVisibleExact);

moneyInSeats.text(moneyMultiplier);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p id="moneyInSeats" class="hotField">20000</p>

最佳答案

代码/计算没问题,您向 UI 打印了错误的值 (moneyInSeats)

    moneyInSeats.text(sectionsVisibleRounded)

演示

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!doctype html>
<html>

<head>
<meta charset="UTF-8">
</head>

<body>
<p id="moneyInSeats" class="hotField">20000</p>
<script>
$(document).ready(function() {
var moneyInSeats = $('#moneyInSeats');
var moneyInSeatsValue = moneyInSeats
var moneyMultiplier = 3;

if (moneyInSeatsValue.text() != '') {
var seatsVisible = ((moneyInSeatsValue.text()) *
moneyMultiplier);
var seatsPerSection = 3250;
var sectionsVisibleExact = seatsVisible / seatsPerSection;
var sectionsVisibleRounded =
Math.floor(sectionsVisibleExact)

moneyInSeats.text(sectionsVisibleRounded)

}
});
</script>
</body>

</html>

关于javascript - 我的计算在 jscript 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49699124/

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