简体   繁体   English

For循环似乎在某些范围内不起作用?

[英]For loop doesn't appear to work in certain ranges?

I have this issue where running a for loop between some ranges of numbers does not work?我有这个问题,在某些数字范围之间运行 for 循环不起作用? The loop literally just does not execute, and I really can't explain why this occurs for some number ranges, but not others.循环实际上只是不执行,我真的无法解释为什么会在某些数字范围内发生这种情况,而在其他数字范围内则不然。 It seems that this only occurs in ranges where the upper value is < 100, and the lower is >=0.似乎这只发生在上限值< 100,下限值> = 0的范围内。 For example 20-100 does not work, yet 40-90 does.例如 20-100 不起作用,但 40-90 起作用。 I've removed a lot of the code (mainly value validation things) that doesn't need to be included, as my only real issue is as to why this loop doesn't work.我删除了很多不需要包含的代码(主要是值验证的东西),因为我唯一真正的问题是为什么这个循环不起作用。

I really don't even know what to try, I can't understand why calling createTable(20,100) doesn't execute the loop, whilst createTable(40,90) does...我真的不知道该尝试什么,我不明白为什么调用 createTable(20,100) 不会执行循环,而 createTable(40,90) 会...

I should also mention that this only occurs when clicking the "convert" button.我还应该提到,这只发生在单击“转换”按钮时。

To try this yourself, enter 20 as the lower value, 100 as the upper value, select "Celsius → Fahrenheit" and click convert.要自己尝试,请输入 20 作为下限值,输入 100 作为上限值,select "Celsius → Fahrenheit" 并单击转换。

 var lowerValue = document.getElementById("lower"); var upperValue = document.getElementById("upper"); var celsiusCheckBox = document.getElementById("celsius"); var fahrenheitCheckBox = document.getElementById("fahrenheit"); var submitButton = document.getElementById("submit"); function onButtonClicked() { createTable(lowerValue.value, upperValue.value) } function createTable(lower, upper) { for(let i = lower; i <= upper; i++ ) { console.log(i); } } function checkCheckBox(element) { if (element.id == "celsius" && fahrenheitCheckBox.checked) { fahrenheitCheckBox.checked = false; } else if (element.id == "fahrenheit" && celsiusCheckBox.checked) { celsiusCheckBox.checked = false; } }
 <;DOCTYPE html> <html lang="en"> <label>Lower value</label> <input type="text" id="lower"> <label>Upper value*</label> <input type="text" id="upper"> <label>Celsius &#8594; Fahrenheit</label> <input type="checkbox" id="celsius" onclick="checkCheckBox(this)"> <label>Fahrenheit &#8594; Celsius</label> <input type="checkbox" id="fahrenheit" onclick="checkCheckBox(this)"> <button type="button" id="submit" onclick="onButtonClicked()">Convert</button> <body> </body> </html>

Any help would be greatly appreciated.任何帮助将不胜感激。

This happens because you are passing strings to the function and "100" is < than "20" since it starts with 1 .发生这种情况是因为您将字符串传递给 function 并且"100" < "20" ,因为它以1开头。

Use利用

function onButtonClicked() {
   const lower = parseInt(lowerValue.value, 10);
   const uppdate = parseInt(upperValue.value, 10);

   createTable(lowerValue.value, upperValue.value)
}

Updated snippet更新的片段

 var lowerValue = document.getElementById("lower"); var upperValue = document.getElementById("upper"); var celsiusCheckBox = document.getElementById("celsius"); var fahrenheitCheckBox = document.getElementById("fahrenheit"); var submitButton = document.getElementById("submit"); function onButtonClicked() { const lower = parseInt(lowerValue.value, 10); const upper = parseInt(upperValue.value, 10); createTable(lower, upper) } function createTable(lower, upper) { for (let i = lower; i <= upper; i++) { console.log(i); } } function checkCheckBox(element) { if (element.id == "celsius" && fahrenheitCheckBox.checked) { fahrenheitCheckBox.checked = false; } else if (element.id == "fahrenheit" && celsiusCheckBox.checked) { celsiusCheckBox.checked = false; } }
 <label>Lower value</label> <input type="text" id="lower"> <label>Upper value*</label> <input type="text" id="upper"> <label>Celsius &#8594; Fahrenheit</label> <input type="checkbox" id="celsius" onclick="checkCheckBox(this)"> <label>Fahrenheit &#8594; Celsius</label> <input type="checkbox" id="fahrenheit" onclick="checkCheckBox(this)"> <button type="button" id="submit" onclick="onButtonClicked()">Convert</button>

The values are received as strings in createTable function, Convert them to integers and try, it will work.这些值在createTable function 中作为字符串接收,将它们转换为整数并尝试,它将起作用。

 var lowerValue = document.getElementById("lower"); var upperValue = document.getElementById("upper"); var celsiusCheckBox = document.getElementById("celsius"); var fahrenheitCheckBox = document.getElementById("fahrenheit"); var submitButton = document.getElementById("submit"); function onButtonClicked() { createTable(lowerValue.value, upperValue.value) } function createTable(lower, upper) { lower = parseInt(lower); upper = parseInt(upper); for(let i = lower; i <= upper; i++ ) { console.log(i); } } function checkCheckBox(element) { if (element.id == "celsius" && fahrenheitCheckBox.checked) { fahrenheitCheckBox.checked = false; } else if (element.id == "fahrenheit" && celsiusCheckBox.checked) { celsiusCheckBox.checked = false; } }
 <;DOCTYPE html> <html lang="en"> <label>Lower value</label> <input type="text" id="lower"> <label>Upper value*</label> <input type="text" id="upper"> <label>Celsius &#8594; Fahrenheit</label> <input type="checkbox" id="celsius" onclick="checkCheckBox(this)"> <label>Fahrenheit &#8594; Celsius</label> <input type="checkbox" id="fahrenheit" onclick="checkCheckBox(this)"> <button type="button" id="submit" onclick="onButtonClicked()">Convert</button> <body> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM