Advanced-Frontend / Daily-Interview-Question

我是依扬(木易杨),公众号「高级前端进阶」作者,每天搞定一道前端大厂面试题,祝大家天天进步,一年后会看到不一样的自己。
https://muyiy.cn/question/
27.43k stars 3.29k forks source link

concatAndSort #541

Closed lyflove closed 3 years ago

lyflove commented 3 years ago

let arrA = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']; let arrB = ['A', 'B', 'C', 'D']; function concatAndSort(arrA,arrB){ let arrC = [].concat(arrA); let j = 0; let count = 0; for(let i=0;i<arrB.length;i++){ console.log(j); while(j<arrA.length){ let astr = arrA[j],bstr = arrB[i]; if(astr.startsWith(bstr)){ j++; }else{ if(j===arrA.length-1){ arrC.push(bstr); break; }else{ arrC.splice(j+count,0,bstr); count++; break; } } }

    }
    return arrC;
}

let arrC = concatAndSort(arrA,arrB); console.log(arrC);