I have a nested loop that will work most of the time, but for one specific case it doesn't start at all.
The following is the value: 1, 3-5, 7-10, 22
JS Code:
document.getElementById("myButton").addEventListener("click", function () { document.getElementById("msg").innerHTML = ""; // Get the short list var list = document.getElementById("myIn").value; var sublists = list.split(", "); var Range = []; var result = ""; var start; // for the nested loop var end; // for the nested loop for (var i = 0; i < sublists.length; i++) { Range = sublists[i].split("-"); start = Range[0]; end = Range[Range.length-1]; Log("Range: " + Range); // Shows which parts of the sublist the program sees for (var j = start; j <= end; j++) { result = result + j + ","; Log("Result in loop: " + result); // Show which parts make it inside the loop } } result = result.slice(0, -1); // Takes off the extra comma at the end Log("Result: " + result); // Shows result });
When an error value is entered, this is the result:
Range: 1 Result in loop: 1, Range: 3,5 Result in loop: 1,3, Result in loop: 1,3,4, Result in loop: 1,3,4,5, Range: 7,10 <--- Never goes inside the loop Range: 22 Result in loop: 1,3,4,5,22, Result: 1,3,4,5,22
I can not understand why the 7-10 part is skipped. Any help or explanation is appreciated.
Here is FIDDLE