如何通过用户输入终止循环(Javascript)

时间:2022-02-12 07:52:32

I am trying to get odd numbers with for loop in javascript but i want to terminate the loop with user input and i cant do this, can you help me friends.

我试图在javascript中使用for循环获取奇数,但我想用用户输入终止循环,我不能这样做,你能帮我朋友吗?

i want " x<7" to be this "x < input"

我希望“x <7”成为这个“x <输入”< p>

<html>

<head>

<body>

<input id="numbers" type="number">
<button onclick="odd()">Odd Numbers</button>
<p id="demo"></p>
<script>
function odd() {
var text = " ";
var x;
for ( x = 1; x < 7; x+= 2) {
    text += x + " ";
}
document.getElementById("demo").innerHTML =+ text;
}
</script>

</body>

</head>

</html>

2 个解决方案

#1


0  

UPDATE

Now that you have told us what you want, here's my updated code:

既然你告诉了我们你想要的东西,这是我的更新代码:

Basically it just sets a limit and changes it with the change of the div

基本上它只是设置一个限制,并随着div的变化而改变它

<html>

<head>

<body>

<input id="numbers" type="number" onChange="change()">
<button onclick="odd()">Odd Numbers</button>
<p id="demo"></p>
<script>
var lim=7;
function change() {
    lim = document.getElementById("numbers").value;
}
function odd() {
var text = " ";
var x;
for ( x = 1; x < lim; x+=2) {
    text += x + " ";
}
document.getElementById("demo").innerHTML += text;
}
</script>

</body>

</head>

</html>

Old code

You can use setInterval and clearInterval to achieve this:

您可以使用setInterval和clearInterval来实现此目的:

var i = 1;
var myInt = setInterval(function(){
document.getElementById("div").innerHTML += i;
i+=2;
}, 100);
<button onClick="clearInterval(myInt)">STOP</button>
<div id="div"></div>

#2


0  

I think this is what you are trying for, This looks at your user input and if it is larger than your starting iterator it loops.

我认为这是你正在尝试的,这看你的用户输入,如果它比你的起始迭代器大,它循环。

const button = document.querySelector('.go');

button.addEventListener('click', e => {
  const input = document.getElementById('numbers');
  const output = document.getElementById('demo');
  
  if (input.value == 0) { return; }
  
  for (let i = 0, len = input.value; i <= len; i++) {
    if (i % 2 === 1) {  
      output.textContent += i.toString();
    }
  }
});
<html>

<head>

<body>

<input id="numbers" type="number">
<button class='go'>Odd Numbers</button>
<p id="demo"></p>


</body>

</head>

</html>

#1


0  

UPDATE

Now that you have told us what you want, here's my updated code:

既然你告诉了我们你想要的东西,这是我的更新代码:

Basically it just sets a limit and changes it with the change of the div

基本上它只是设置一个限制,并随着div的变化而改变它

<html>

<head>

<body>

<input id="numbers" type="number" onChange="change()">
<button onclick="odd()">Odd Numbers</button>
<p id="demo"></p>
<script>
var lim=7;
function change() {
    lim = document.getElementById("numbers").value;
}
function odd() {
var text = " ";
var x;
for ( x = 1; x < lim; x+=2) {
    text += x + " ";
}
document.getElementById("demo").innerHTML += text;
}
</script>

</body>

</head>

</html>

Old code

You can use setInterval and clearInterval to achieve this:

您可以使用setInterval和clearInterval来实现此目的:

var i = 1;
var myInt = setInterval(function(){
document.getElementById("div").innerHTML += i;
i+=2;
}, 100);
<button onClick="clearInterval(myInt)">STOP</button>
<div id="div"></div>

#2


0  

I think this is what you are trying for, This looks at your user input and if it is larger than your starting iterator it loops.

我认为这是你正在尝试的,这看你的用户输入,如果它比你的起始迭代器大,它循环。

const button = document.querySelector('.go');

button.addEventListener('click', e => {
  const input = document.getElementById('numbers');
  const output = document.getElementById('demo');
  
  if (input.value == 0) { return; }
  
  for (let i = 0, len = input.value; i <= len; i++) {
    if (i % 2 === 1) {  
      output.textContent += i.toString();
    }
  }
});
<html>

<head>

<body>

<input id="numbers" type="number">
<button class='go'>Odd Numbers</button>
<p id="demo"></p>


</body>

</head>

</html>