728x90
#문제
배열을 입력받아 가장 큰 요소를 리턴해야 합니다.
#예시
let output = getLargestElement([1, 4, 3]);
console.log(output); // --> 4
output = getLargestElement([-4, -2, -9]);
console.log(output); // --> -2
#정답
function getLargestElement(arr) {
let max = arr[0];
for(let i=0; i<arr.length; i++){
if (max < arr[i]){
max= arr[i];
}
}
return max;
}
728x90
'코플릿 기록 > JavaScript' 카테고리의 다른 글
객체 6번 (0) | 2023.01.23 |
---|---|
배열 9번 getLongestWord (0) | 2023.01.23 |
배열 1번 (0) | 2023.01.23 |
데일리코딩 7번 convertListToObject (0) | 2023.01.22 |
반복문9번★★ (0) | 2023.01.16 |