fetch variable from another function for loop
I want to fetch the OTP variable inside for loop from the first function to the second function to verify with other variable values.
function validate() {
    var a = document.getElementById("mobile").value;
    if (a == "" || a.length > 10 || a.length < 10) {
        document.getElementById("message").innerHTML = "Enter Valid Mobile Number";
        return false;
    }
    
    if (a.length == 10) {
        var elemB = document.getElementById("mobile").value;
        const url = 'http://samplereques/WRequest?RequestType=UI&phoneNo=' + elemB;
        {
            $.getJSON(url, function (data) {
                for (i = 0; i < data.length; i = i + 1) {
                    otp = data[i].OTP;
                    console.log(otp)
                }
            });
        }
    }
}
function otpvalidate() {
    var ot = document.getElementById("mobile").value;
    if (ot == "" || ot.length > 10 || ot.length < 10) {
        document.getElementById("otpreturn").innerHTML = "Enter Valid OTP Number";
        return false;
    }
    if (ot == otp) {
        alert("sucess");
        console.log("success");
    }
}
I want to fetch the OTP variable inside for loop from the first function to the second function to verify with other variable value.
 
    