递归练习

//max值
function getMaxVal(arr){
    let maxVal=null;
    function loop(arr){
        let [key1,...keys]=arr;
        if(!key1){return}
        maxVal=key1>maxVal?key1:maxVal;
        loop(keys);
    }
    loop(arr)
    return maxVal;
}
getMaxVal([9,12,1,3,6])

function getMaxVal1(arr){
    function loop(arr){
        let [key1,...keys]=arr;
        if(!keys.length){return key1}
        return key1>loop(keys)?key1:loop(keys);
    }
    return loop(arr)
}
getMaxVal1([9,12,1,3,6])
let myReverse=(arr)=>{
    let [key1,...keys]=arr;
    if(!keys.length)return [key1]
    return [...myReverse(keys),key1]
}
myReverse(['a','b','c'])

 

上一篇:科普笔记


下一篇:查询优化的百科全书 —— Explain 详解(下)