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));