반응형
문제 설명
머쓱이는 40살인 선생님이 몇 년도에 태어났는지 궁금해졌습니다. 나이 age가 주어질 때, 2022년을 기준 출생 연도를 return 하는 solution 함수를 완성해주세요.
내가 한 문제 풀이
function solution(age) {
var answer = 2022 - age + 1;
return answer;
}
다른 사람이 한 문제 풀이 1
function solution(age) {
return new Date().getFullYear() - age + 1;
}
새로 배운 함수 new Date
기본 구문
new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
매개변수
Date() 생성자는 네 가지 형태로 사용할 수 있음.
1. 매개 변수 없음
new Date ()
생성 순간의 날짜와 시간을 나타내는 Data 객체를 생성.
2. UNIX 타임 스탬프 값
new Date (value)
1970년 1월 1일 00:00:00 UTC(UNIX 시간)부터의 시간을 밀리초 단위로 표현하되 윤초는 무시한 정수 값
3. 타임스탬프 문자열
new Date (dateString)
4. 개별 날짜 및 시간 구성 요소
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
새로 배운 함수 getFullYear()
// 기본 구문
dateObj.getFullYear()
// dateObj => 변환 가능.
// ex)
// var today = new Date();
// var year = today.getFullYear();
반환 값
현지 시간에 따라, 주어진 날짜의 연도에 해당하는 숫자.
설명
getFullYear()가 반환하는 값은 절댓값.
getFullYear()는 1000년과 9999년 사이의, 1995년과 같은 날짜에 대해서는 네 자리 숫자를 반환.
이 메서드를 사용해야 2000년 이후의 날짜에 대해서도 호환 가능.
다른 사람이 한 문제 풀이 2
function solution(age) {
return 2022-age+1;
}
function solution(age) {
return 2023-age
}
const solution = (age) => 2022 - age + 1반응형
'코딩 테스트 리뷰' 카테고리의 다른 글
| [입문] 몫 구하기 // parseInt, Math.floor, Math.trunc, ~~ (0) | 2022.10.07 |
|---|