정수 x가 주어졌을때, x가 palindrome이면 true를 아니면 false를 리턴하라.
여기서 palindrome이란 앞으로 읽었을때랑 뒤로 읽었을때랑 값이 같은 정수이다.
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
var isPalindrome = function(x) {
return String(x) === [...String(x)].reverse().join('');
};
number타입인x를string타입으로 전환한 후 배열로 전환한다.
그리고reverse메소드를 사용하여 순서를 뒤집은 후join을 이용해서 요소들을 이어붙여string타입으로 전환한다.
string타입인x와 뒤집은x는 원시타입이라===비교가 가능하다.
개발자로서 배울 점이 많은 글이었습니다. 감사합니다.