我是一名11年级学生,需要我老师指定的forloop作业的帮助。
代码的问题在于输出发布,但它在消失之前只出现了几毫秒。
function loops1() {
var inputV = document.getElementById('input1').value;
var inputINT = parseInt(inputV);
var show = "";
try {
if (inputV == "") throw "is empty";
if (isNaN(inputV)) throw 'must be a number';
} catch (err) {
alert("Input field " + err);
return;
}
for (var i = 1; i <= inputINT; i++) {
show += i + '<br>';
}
document.getElementById('showN').innerHTML = 'Numbers in order from 1 to n:' + '<br>' + show;
}
function loops2() {
var inputV = document.getElementById('input2').value;
var inputINT = parseInt(inputV);
var sum = 0;
try {
if (inputV == "") throw "is empty";
if (isNaN(inputV)) throw 'must be a number';
} catch (err) {
alert("Input field " + err);
return;
}
for (var i = 1; i <= inputINT; i++) {
sum += i;
}
document.getElementById('showN').innerHTML = 'Sum of numbers from 1 to n:' + '<br>' + sum;
}
function loops3() {
var inputV = document.getElementById('input3').value;
var inputINT = parseInt(inputV);
var sumE = 0;
try {
if (inputV == "") throw "is empty";
if (isNaN(inputV)) throw 'must be a number';
} catch (err) {
alert("Input field " + err);
return;
}
for (var i = 1; i <= inputINT; i++) {
if ((i % 2) == 0) sumE += i;
}
document.getElementById('showN').innerHTML = 'Sum of even numbers from 1 to n:' + '<br>' + sumE;
}
function loops4() {
var inputV = document.getElementById('input4').value;
var inputINT = parseInt(inputV);
var factorial = 1;
try {
if (inputV == "") throw "is empty";
if (isNaN(inputV)) throw 'must be a number';
} catch (err) {
alert("Input field " + err);
return;
}
for (var i = 1; i <= inputINT; i++) {
factorial = factorial * i;
}
document.getElementById('showN').innerHTML = 'Factorial of n:' + '<br>' + factorial;
}
function loops5() {
var sports = ["Cricket", "Volleyball", "Basketball", "Hockey", "Soccer", "Football"];
var sportsD = "";
var lenA = sports.length
for (var i = 0; i < lenA; i++) {
sportsD += sports[i] + '<br>';
}
document.getElementById('showN').innerHTML = 'List of Sports:' + '<br>' + sportsD;
}
&#13;
<h1>LOOPS</h1>
<form>
Write all numbers between 0 and input<br>
<input type='text' id='input1' placeholder='enter number'><br>
<button onclick='loops1()' type='submit'>Submit</button><br><br> Write sum of numbers 1 to input<br>
<input type='text' id='input2' placeholder='enter number'><br>
<button onclick='loops2()' type='submit'>Submit</button><br><br> Write sum of even numbers from 1 to input<br>
<input type='text' id='input3' placeholder='enter number'><br>
<button type='submit' onclick='loops3()'>Submit</button><br><br> Write factorial of input<br>
<input type='text' id='input4' placeholder='enter number'><br>
<button type='submit' onclick='loops4()'>Submit</button><br><br>
<button type='button' onclick='loops5()'>Display Array</button>
</form>
<div id='showN'></div>
&#13;
答案 0 :(得分:2)
发生了什么,因为你把所有东西放在一个表格中,而你的按钮都有一个&#34;提交&#34;类型,浏览器正在尝试提交&#34;你的表格。由于您没有设定目标或方法,因此只需刷新页面即可。
Write all numbers between 0 and input<br>
<input type='text' id='input1' placeholder='enter number'><br>
<button onclick='loops1()'>Submit</button><br><br> Write sum of numbers 1 to input<br>
<input type='text' id='input2' placeholder='enter number'><br>
<button onclick='loops2()'>Submit</button><br><br> Write sum of even numbers from 1 to input<br>
<input type='text' id='input3' placeholder='enter number'><br>
<button onclick='loops3()'>Submit</button><br><br> Write factorial of input<br>
<input type='text' id='input4' placeholder='enter number'><br>
<button onclick='loops4()'>Submit</button><br><br>
<button onclick='loops5()'>Display Array</button>
如果没有表单标签,则应该为您提供所需的结果。