簡體   English   中英

For循環似乎在某些范圍內不起作用?

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

我有這個問題,在某些數字范圍之間運行 for 循環不起作用? 循環實際上只是不執行,我真的無法解釋為什么會在某些數字范圍內發生這種情況,而在其他數字范圍內則不然。 似乎這只發生在上限值< 100,下限值> = 0的范圍內。 例如 20-100 不起作用,但 40-90 起作用。 我刪除了很多不需要包含的代碼(主要是值驗證的東西),因為我唯一真正的問題是為什么這個循環不起作用。

我真的不知道該嘗試什么,我不明白為什么調用 createTable(20,100) 不會執行循環,而 createTable(40,90) 會...

我還應該提到,這只發生在單擊“轉換”按鈕時。

要自己嘗試,請輸入 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>

任何幫助將不勝感激。

發生這種情況是因為您將字符串傳遞給 function 並且"100" < "20" ,因為它以1開頭。

利用

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

   createTable(lowerValue.value, upperValue.value)
}

更新的片段

 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>

這些值在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