JavaScript

콜백 사용 교육 22.08.08

연93 2022. 8. 8. 21:22

//max(1,2 3,4) 형태와 max([1, 2, 3, 4])형태를
    //모두 처리 할 수 있는 max함수를 만드시오.
    //max() null
    //max([]) null
    
    const max = function(...ar)
    {
        let maxValue = 0;
                    
        const calcMax = function(ar)
        {
            let maxValue = ar[0];
            for (let i = 1; i < ar.length; i++)
            {
                if (maxValue < ar[i])
                {
                    maxValue = ar[i];
                }
            }
            return maxValue;
        }
        if ((ar.length === 0) || (ar[0].length === 0))
        { 
            return null;
        }
        if (typeof(ar[0]) === 'number')
        {
            maxValue = calcMax(ar);
            
        }
        else if(Array.isArray(ar[0]) === true)
        {
            maxValue = calcMax(ar[0]);
        }
        else
        {
            maxValue = null;
        }
        return maxValue;            
    }
    console.log(max([1, 2, 3]));
    console.log(max(22, 1, 2, 3));

'JavaScript' 카테고리의 다른 글

객체연습  (0) 2022.08.09
forEach.map 실습 22.08.09  (0) 2022.08.09
띠 구하는 방법 22 08.05  (0) 2022.08.05
국비지원 시험 HTML,CSS,JS활용 08.04  (0) 2022.08.04
별찍기,배열 continue,break문 연습 08.02  (0) 2022.08.02