Logic for the implementation of “select / unselect” checkbox sequencially in Java script

I have a main JSP Page which shows the Club (lottery) information like Plus icon, Lottery number, lucky number, purchase date, others for the search criteria. This page can have Single club/ Multiple clubs

There is an dynamic jsp page which has been included in the main page which shows the installment details and checkboxes and dynamic jsp code written in Javascript (jsp page get created when we click on PLUS Icon in the main page).
In the dynamic jsp page there are check box which need to be checked/unchecked sequentially by the user.
A validation is needed to be implemented for the above. I have tried multiple logic but its not working.

This code doesn’t work in all condition. For example,
Scenario 1) When uncheck the middle checkbox alert message shown buy checkbox got unchecked.
If fix one issue it creates another issue.

Lastly I have posted here for support. Thanks in Advance.

Javascript Code:

function CheckedOrNot(lotNo, k) {

// here lotNo is the lottery number and k is the position of the checkbox

var checkbox = document.getElementById("chckBox_" + lotNo + "_" + k);

// totalcheckbox is the number of checkboxes for each lottery. Max Value of totalcheckbox  is 24 and minimum is 1)

var totalcheckbox = document.getElementById("totalInstallment_" + lotNo).value;

// in case of first checkbox it should validate lock/unlock of that lottery and do required steps 
if (k == 0) {
    validateLotteryLock(lotNo, checkbox, k);
}

// Sequential checkbox checking validation
for (var i = 0; i < k; i++) {
    var prevCheckbox = document.getElementById("chckBox_" + lotNo + "_" + i);
    if (!prevCheckbox && k !== 1) {
        alert("You haven't paid previous dues. Please select installments sequentially.");
        document.getElementById("chckBox_" + lotNo + "_" + i) = prevCheckbox;
        return false;
    }
}

// If the second installment is checked, prevent unchecking the first installment
if (k == 1 && checkbox) {
    var firstCheckbox = document.getElementById("chckBox_" + lotNo + "_0");
    var firstCheckboxVal = firstCheckbox.checked;
    if (firstCheckboxVal && !checkbox) {
        alert("You cannot uncheck the first installment while the second installment is checked.");
        firstCheckbox.checked = true; // Revert the first checkbox state back to checked
        return false; // Stop further execution
    }
}

// Display alert if checkbox is unchecked without paying previous dues
if (!checkbox) {
    alert("You have unpaid previous dues. Please pay previous installments before selecting the current one.");
    return false; // Stop further execution
}

var checkboxTrueResult = 0;
var checkboxFalseResult = 0;
for (var i = totalcheckbox; i > 0; i--) {
   var prevCheckbox = document.getElementById("chckBox_" + lotNo + "_" + i);
   
   if (prevCheckbox) {
        //alert("prevCheckbox is true");
        checkboxTrueResult = checkboxTrueResult + 1;
   }
   
   if (!prevCheckbox) {
        checkboxFalseResult = checkboxFalseResult + 1;
        //alert("prevCheckbox is false");
   }
}
   
   if (checkboxTrueResult > 0 && checkboxFalseResult == 0) {
        alert("1");
   }
   
   if (checkboxFalseResult > 0 && checkboxTrueResult == 0) {
        alert("2");
   }
// Calculate total amount for the selected installment
calculateClubAmount(lotNo, k);
}

For reference, Please refer the screenshots:

Main Page
Main Page

Main Page with Dynamic Page having checkboxes
MainPage with Dynamic Table