How to merge two function into one function

how do two merge into one?
function rightClick () {

    
    index -= imgCount;
    
    
    if(index < 0)
    {
        index = 0;
        return;
    
    }
    document.getElementById("img1").setAttribute("src", imgs[(index)%3]);
    document.getElementById("img2").setAttribute("src", imgs[(index + 1)%3]);
    document.getElementById("img3").setAttribute("src", imgs[(index+2)%3]);
}
function leftClick(){
    index += imgCount;
    if(index > imgs.length){
        index = imgs.length;
        return;
    }
    document.getElementById("img1").setAttribute("src", imgs[(index)%3]);
    document.getElementById("img2").setAttribute("src", imgs[(index +1)%3]);
    document.getElementById("img3").setAttribute("src", imgs[(index+2)%3]);
    
}
Mar.01,2021

conditional judgment

function leftClick(){   //  event 
    if(){
     //
    }
    if(){
     //
    }
    document.getElementById("img1").setAttribute('src', imgs[(index)%3]);
    document.getElementById("img2").setAttribute('src', imgs[(index +1)%3]);
    document.getElementById("img3").setAttribute('src', imgs[(index+2)%3]);
    
}

/**
 * type leftright
 * 
 * @param {any} type 
 * @returns 
 */
function setImage(type){
    if(type=='left'){
        index += imgCount;
        if (index > imgs.length) {
            index = imgs.length;
            return;
        }
    }else if(type=='right'){
        index -= imgCount;
        if (index < 0) {
            index = 0;
            return;
        }
    }
    document.getElementById("img1").setAttribute('src', imgs[(index) % 3]);
    document.getElementById("img2").setAttribute('src', imgs[(index + 1) % 3]);
    document.getElementById("img3").setAttribute('src', imgs[(index + 2) % 3]);
}

setImage ('left') or setImage (' right')


function, you can add parameters. Hhh found that after this point, many methods can be integrated into a function (of course, the premise is based on business division)
in which your two click actually do the same thing. You can pass a reference to whether the tag is leftClick or rightClick.
in the call time. In summary:

function click(flag){
    // flag1leftClick
    if (flag) {
        index += imgCount;
        if(index > imgs.length){
            index = imgs.length;
            return;
        }
    }else{     // flag0rightClick
       index -= imgCount;
        if(index < 0)
        {
            index = 0;
            return;
        } 
    }
    document.getElementById("img1").setAttribute('src', imgs[(index)%3]);
    document.getElementById("img2").setAttribute('src', imgs[(index +1)%3]);
    document.getElementById("img3").setAttribute('src', imgs[(index+2)%3]); 
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-7b510f-264c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-7b510f-264c9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?