其他分享
首页 > 其他分享> > 递归练习

递归练习

作者:互联网

//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'])

 

标签:maxVal,arr,return,递归,keys,key1,练习,loop
来源: https://www.cnblogs.com/little-ab/p/11522983.html