[Javascript] 프로그래머스 Level 2 _ 최댓값과 최솟값
👇🏻

👇🏻
시행착오
( + 코드를 작성하면서 든 생각들.)
✏️ 제출 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(s) { | |
s = s.split(' ').sort((a,b) => a-b); | |
let answer = [(s[0]), s[s.length-1]]; | |
return answer.join(' '); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(s) { | |
s = s.split(' '); | |
const max = s.reduce((a,b) => { return Math.max(a,b) }); | |
const min = s.reduce((a,b) => { return Math.min(a,b) }); | |
return `${min} ${max}`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(s) { | |
s = s.split(' '); | |
const max = Math.max.apply(null, s); | |
const min = Math.min.apply(null, s); | |
return `${min} ${max}`; | |
} | |
// +) ES6 Spread Operator 사용 시 ! | |
function solution(s) { | |
s = s.split(' '); | |
const max = Math.max(... s); | |
const min = Math.min(... s); | |
return `${min} ${max}`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(s) { | |
s = s.split(' ').map((el) => parseInt(el)); | |
let max = s[0]; | |
let min = s[0]; | |
for(let i=1; i < s.length; i++){ | |
(s[i] > max)? max = s[i] : (s[i] < min)? min = s[i] : ''; | |
} | |
return `${min} ${max}`; | |
} |
오늘은 Level 2의 다른 문제들을 풀어보려고 하다가 ,,,,
동적 계획법에 좌절하고 제일 쉬워 보이는 걸 풀었다 ㅎㅎㅎㅎㅎㅎㅎㅎㅎ
그래도 배열에서 Math.max(), Math.min()을 쓸 수 있게 해 준
잘 써보지 않았던 apply() 함수를 사용해 볼 수 있었다,,, 하하하 🥲
+)
배열에서 Math.max(), Math.min()을 사용하고 싶을 때,
ES6 문법인 스프레드 연산자를 사용하면 apply()를 사용하지 않고도 사용할 수 있다!
참고로 코드에 추가했다!
⚠️ 아래 내용은 모두 개인적인 참고 / 기록을 위한 용도입니다. 참고해주시고 편안하게 봐주세요 :) ⚠️
*** 혹시라도 잘못된 정보가 있다면 언제든지 알려주시면 감사하겠습니다 ! ***
'Programmers > Level 2' 카테고리의 다른 글
[Javascript] 프로그래머스 : 기능 개발 (0) | 2021.08.22 |
---|---|
[Javascript] 프로그래머스 : 올바른 괄호 (0) | 2021.08.22 |
[Javascript] 프로그래머스 : 행렬의 곱셈 (0) | 2021.08.20 |
[Javascript] 프로그래머스 : 프린터 (0) | 2021.08.19 |
[Javascript] 프로그래머스 : 최솟값 만들기 (0) | 2021.08.18 |