你好,我正试图通过使用一个带有起始编号和平均数的表格来显示一个平均值的javascript。用户将输入起始编号并输入偶数显示的编号。 说从7开始的数字是3应该显示8 10 12.只需要一些方向。感谢
我的代码:
<html>
<head>
<title>evens</title>
<div class="container">
<h1>Enter Number</h1>
<form name="number" class="form-inline">
<div class="form-group">
<label for="starting">Starting Number</label>
<input type="text" id="starting" class="form-control">
</div> <div class="form-group">
<label for="step">numbers of evens</label>
<input type="text" id="step" class="form-control">
</div>
<button type="button" onclick="evenDisplay()" class="btn" >submit</button>
<script>
function evenDisplay() {
var starting = document.getElementById("starting").value;
var step = document.getElementById("step").value;
var evens = [];
for (i = starting; i <=12; i++) { /* this loop would show as much as evens number as long value of i<= set to and doesn't care if the user enter the number of evens */
i % 2 || evens.push(i);
}
console.log(evens)
}
</body>
</html>
答案 0 :(得分:0)
for (i = starting; step==evens.length ;i++) {
i % 2 || evens.push(i);
}
答案 1 :(得分:0)
一切看起来都不错,但是你可能会更好地使用while循环,因为你想循环直到evens
包含step
值。
var currentVal = starting;
while(evens.length < step){
currentVal % 2 || evens.push(currentVal);
currentVal++;
}
答案 2 :(得分:0)
你在javascript中使用comas,你应该使用半冒号并且你缺少许多标签。请参阅下面的更正代码。我在评论中遗漏了一些内容:
<!DOCTYPE html >
<head>
<title>evens</title>
<script>
function evenDisplay() {
var starting = document.getElementById("starting").value; //SEMI-COLON HERE
var step = document.getElementById("step").value; //SEMI-COLON HERE
var evens = [];
for (i = starting; i <=12; i++) {
i % 2 || evens.push(i);
}
console.log(evens); //SEMI-COLON HERE
}
</script> <!--you had no closing script tag-->
</head> <!--you had no closing head tag-->
<body> <!--you had no opening body tag-->
<div class="container">
<h1>Enter Number</h1>
<form name="number" class="form-inline">
<div class="form-group">
<label for="starting">Starting Number</label>
<input type="text" id="starting" class="form-control">
</div> <div class="form-group">
<label for="step">numbers of evens</label>
<input type="text" id="step" class="form-control">
</div>
<button type="button" onclick="evenDisplay()" class="btn" >submit</button>
</form> <!--CLOSING FORM TAG HERE-->
</div>
</body>
</html>
答案 3 :(得分:0)
您的代码中有几个错误。我想我修好了:
function evenDisplay() {
var starting = parseInt(document.getElementById("starting").value);
var step = parseInt(document.getElementById("step").value);
var evens = [];
for (i = starting+(starting%2), j=0; j <step; i+=2, j++) {
evens.push(i);
}
console.log(evens);
}
window.onload = function(){
document.getElementById("btn").onclick = evenDisplay;
}
您需要在按钮中添加id =“btn”才能生效。
非常不鼓励使用HTML中的Btw definind事件处理程序。
答案 4 :(得分:0)
function evenDisplay() {
var starting = parseInt(document.getElementById("starting").value);
var step = parseInt(document.getElementById("step").value);
if(!isNaN(starting)&&!isNaN(step)){
var evens = [];
var x=(step*2)+starting;
for (i = starting+1; i <=x; i++) { /* this loop would show as much as evens number as long value of i<= set to and doesn't care if the user enter the number of evens */
i % 2 || evens.push(i);
}
console.log(evens);
}
else{console.log("Nan");}
}
<div class="container">
<h1>Enter Number</h1>
<form name="number" class="form-inline">
<div class="form-group">
<label for="starting">Starting Number</label>
<input type="text" id="starting" class="form-control">
</div> <div class="form-group">
<label for="step">numbers of evens</label>
<input type="text" id="step" class="form-control">
</div>
<button type="button" onclick="javascript:evenDisplay();" class="btn" >submit</button>
这里我使用等式=(步骤* 2)+开始在所需范围内循环也检查输入值是否为isNaN