728x90
반응형

1. 문제
프로그래머스 알고리즘 문제풀이 - 자바스크립트 [ 가장 큰 수 찾기 ]

2. 시도해본 것들

정말 오랜만에 보자마자 풀이법이 떠오른건 처음이었다.

가장 큰수 = Math.max()

인덱스값 = indexOf()

 

4. 알게 된 점

나의 코드

function solution(array) {
    const idx = array.indexOf(Math.max(...array))
    const maxValue = Math.max(...array)
    return [maxValue, idx];
}

1분만에 풀 수 있었다! 맨날 머리싸메다 상큼하게 푸니 기분좋았다.

보통 Math.max(1,2,3) 이런식이겠지만 배열이 들어간다면

스프레트 연산자를 이용하여 Math.max(...array)로 넣어줘야 한다.

 

 

남의 코드

function solution(array) {
    let max = Math.max(...array);
    return [max, array.indexOf(max)];
}

같은 풀이지만 더 짧게~

728x90
반응형

+ Recent posts