카테고리 없음
[LeetCode] 1185. Day of the Week - 자바스크립트 0623
codevil
2022. 6. 23. 11:17
https://leetcode.com/problems/day-of-the-week/
Day of the Week - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
자바스크립트의 new Date를 이용하여, 요일을 얻고 문제에 주어진 array를 이용하여 요일을 return 하는 문제이다.
단지 문제를 1회차 풀이에서 틀렸었는데 주의를 해야하는 것이
new Date함수이다. new Date함수의 모양을 잘봐보자!
new Date(year, month index, day)
month는 잘보면 month index이다 즉 -1을 해줘야한다!
이전에 써봤었던 new Date인데 막상 문제로 보니 ;ㅁ; 이렇게 사용하는 구나 하고 다시 한번 확인할 수 있었다.
전체 풀이
var dayOfTheWeek = function (day, month, year) {
const dayList = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
return dayList[new Date(year, month - 1, day).getDay()]
};