Javascript optimal solution with less number of lines. Compiled successfully


#1

function(A, B, C){
const cArray = C.toString().split(’’).map(num => parseInt(num));

    if(B < cArray.length) {
        const index = A[0] === 0 && B !== 1 ? A.length - 1 : A.length; 
        return index*Math.pow(A.length, B - 1);
    } else if( B > cArray.length) {
        return 0;
    } else {
        let sols = 0;
        let i = 0;
    
        while(i < B) {
            let index = A.findIndex(num => num > cArray[i]);
            index = index === -1 ? A.length : index;

            let newIdx = cArray[i] === A[index - 1] ? index - 1 : index;
            if(i === 0 && A[0] === 0 && B !== 1) {
                newIdx -= 1;
            }
            sols += newIdx*Math.pow(A.length, B - i - 1);
               
            if(cArray[i] !== A[index - 1]) {
                return sols;
            }

            i++;
        }

        return sols;
    }
}