Error in a script for Adobe Illustrator in charge of numbering

I’m working on a JavaScript script for Adobe Illustrator that will number multiple text fields in an.ai. The script should increment the numbering in multiple text fields at the same time and generate multiple copies of the document.

The script works well for the first text field, increasing the numbering from 0001 to 0005 as expected. However, for the second field, which must be numbered 0006 to 0010, the script stops at 0008 and continues printing the same number until the prints in the first field are completed. Then, move to the next number for the second field, but still with the same problem.

"use strict";

// Request the number of fields to number
var fieldCount = parseInt(prompt("Enter the number of fields to number:", "4"));
var fields = [];

// Request the number of copies to print
var numberOfCopies = parseInt(prompt("How many copies would you like to print? (e.g., for Original and Copy enter 2):", "2"));

// Configure fields with their numbering range
for (var i = 0; i < fieldCount; i++) {
    var fieldName = prompt("Enter the name of field " + (i + 1) + ":", "field_" + (i + 1)); // Field name
    var initialNumber = parseInt(prompt("Enter the starting number for field " + (i + 1) + ":", "0001"));
    var finalNumber = parseInt(prompt("Enter the final number for field " + (i + 1) + ":", "0005"));

    if (isNaN(initialNumber) || isNaN(finalNumber) || finalNumber < initialNumber) {
        alert("Please enter valid numbers.");
        exit();
    }

    fields.push({
        name: fieldName,
        current: initialNumber,
        last: finalNumber
    });
}

// Select Adobe Illustrator file
var baseFile = File.openDialog("Select the base file (.ai)");
if (baseFile === null) {
    alert("No file was selected.");
    exit();
}
var document = app.open(baseFile);

// Check if there are enough textFrames in the document
if (document.textFrames.length < fieldCount) {
    alert("The document does not have enough text fields. Please check the file.");
    exit();
}

// Loop to number and generate the copies
var continuePrinting = true;
while (continuePrinting) {
    continuePrinting = false;
    for (var i = 0; i < fields.length; i++) {
        // Find the corresponding textFrame by name
        var foundTextFrame = null;
        for (var j = 0; j < document.textFrames.length; j++) {
            if (document.textFrames[j].name === fields[i].name) {
                foundTextFrame = document.textFrames[j];
                break;
            }
        }
        if (foundTextFrame === null) {
            alert("No text field found with the name: " + fields[i].name);
            exit(); 
        }
        if (fields[i].current <= fields[i].last) {
            continuePrinting = true; // There are still fields to number
            var currentNumber = ("0000" + fields[i].current).slice(-4);
            foundTextFrame.contents = "No. " + currentNumber;
        }
    }

    // Print all updated fields before continuing numbering
    if (continuePrinting) {
        for (var copy = 0; copy < numberOfCopies; copy++) {
            document.print();
        }

        // Increment the numbers of all fields after printing
        for (var i = 0; i < fields.length; i++) {
            if (fields[i].current <= fields[i].last) {
                fields[i].current++; // Increment the field at the same time
            }
        }
    }
}
alert("Process completed. The receipts were printed in the specified fields.");

The code’s goal is for text fields in Adobe Illustrator to be numbered synchronously and in parallel. For example, if you have two fields with numbering ranges of 0001-0005 and 0006-0010, both should advance at the same time and in a coordinated way, such as 0001 and 0006, 0002 and 0007, etc. After updating the numbering in all the fields, the document should be printed according to the specified number of copies, so that each copy reflects the current numbering of all fields. Once the copies have been generated, the numbers in all fields must be incremented simultaneously to continue the sequence on the next print cycle.