5_hyun
2022. 8. 13. 23:20
반응형
해결법
-for문으로 해결
우선 대소문자 구별을 하지 않으니까 대문자나 소문자로 통일해야 한다. 그리고 for문으로 문자 길이의 절반만 돌면서 자기와 대칭되는 문자가 같은지 다른지 보면된다.
코드
-for문으로 해결
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s) {
let answer = "YES";
s = s.toLowerCase();
for (let i = 0; i < Math.floor(s.length / 2); i++) {
if (s[i] !== s[s.length - i - 1]) {
answer = "NO";
break;
}
}
return answer;
}
let str = "gooG";
console.log(solution(str));
</script>
</body>
</html>
해결법
-함수로 푸는법
마찬가지로 먼저 대소문자 통일을 해야한다. 그리고 문자를 배열로 쪼개고 거꾸로 정렬을 하고 다시 문자열로 만들어주면 된다. 이때 join() 함수가 사용된다.
-join()
join 함수는 배열을 문자열로 만들어 줄 수 있다.
const arr = ['바람', '비', '물'];
console.log(arr.join());
// 바람,비,물
console.log(arr.join(''));
// 바람비물
console.log(arr.join('-'));
// 바람-비-물
이런식으로 사용이 가능하다.
코드
-함수로 푸는법
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s) {
let answer = "YES";
s = s.toLowerCase();
tmp = s.split("").reverse().join("");
if (tmp !== s) answer = "NO";
return answer;
}
let str = "gooG";
console.log(solution(str));
</script>
</body>
</html>
반응형