gpt4 book ai didi

javascript - 如何让模数运算符在 JavaScript 中的两个数字之间添加奇数?

转载 作者:行者123 更新时间:2023-12-03 09:53:59 26 4
gpt4 key购买 nike

以下是我到目前为止所拥有的。我是将用户提示放在函数中还是将其保留在原处?我“试图”完成的是添加

<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<script type="text/javascript">
/* Input: user types in an integer
* Processing: adds all odd numbers between input and 0
* Output: the sum of the odd numbers
*/
function addOdds() {
var n = parseInt(document.getElementById('number').value);
var sum = i=i+2;
for **(var i = -1; i < 6; i = i + 2)** {
if (n == 0) {
break;
}
sum += n;
}
window.alert(sum);
}
</script>
</head>

<body>
Please enter a number. <input type="text" id="number">
<button type="button" onclick="addOdds()"> Get the sum </button>
<div id=""> </div> <!--is this part need? -->
</body>
</html>

最佳答案

var sum = 0;
for (var i = 1; i<=n; i+=2) {
sum+=i;
}

关于javascript - 如何让模数运算符在 JavaScript 中的两个数字之间添加奇数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30795377/

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