프로그래머스 연습문제 - 최댓값과 최솟값(level2, JS)

j_wisdom_h·2023년 1월 27일
0

CodingTest

목록 보기
26/58

프로그래머스 연습문제 - 최댓값과 최솟값(level2, JS)

문제설명

제한조건 & 입출력 예

My solution

js

function sol(s) {
    s = s.split(' ');
    s.sort((a, b) => parseInt(a) - parseInt(b));
    return `${s[0]} ${s[s.length - 1]}`;
}
//또는
function solution(s) {  
    s = s.split(' ')
    return `${Math.min(...s)} ${Math.max(...s)}`;
}

공부한 것

js 배열의 마지막 요소

  1. Array.length - 1
  2. Array.slice(-1)[0] : 문자열 자르기
  3. Array.at() : 신규 함수이기 때문에 호환성 문제가 생길 수 있음
  4. Array.pop()

js Math

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/max

console.log(Math.max(1, 3, 2));
// Expected output: 3

console.log(Math.max(-1, -3, -2));
// Expected output: -1

const array1 = [1, 3, 2];

console.log(Math.max(...array1));
// Expected output: 3
profile
뚜잇뚜잇 FE개발자

0개의 댓글