대문자로 통일
대문자와 소문자가 같이 존재하는 문자열을 입력받아 대문자로 모두 통일하여 문자열을 출력
하는 프로그램을 작성하세요.
▣ 입력설명
첫 줄에 문자열이 입력된다. 문자열의 길이는 100을 넘지 않습니다.
▣ 출력설명
첫 줄에 대문자로 통일된 문자열이 출력된다.
▣ 입력예제 1
ItisTimeToStudy
▣ 출력예제 1
ITISTIMETOSTUDY
<!--my solution-->
<html>
<head>
<meta charset="UTF-8">
<title>출력결과 - 섹션1 - 12 - 대문자로 통일</title>
</head>
<body>
<script>
function solution(str) {
let answer = [];
answer.push(str.toUpperCase());
return answer.join();
}
let str = "BanAna";
console.log(solution(str));
</script>
</body>
</html>
잘 나온다ㅇㅇ 근데 뭔가 계속 같은 코드로 짜고있는 느낌이다.
이게 처음에 answer을 '' 으로 초기화 하거나 선언만 해줬더니 error가 났다.
그래서 배열에 push하고 join으로 배열을 벗겼다.
<!--teacher's solution-->
<html>
<head>
<meta charset="UTF-8">
<title>출력결과</title>
</head>
<body>
<script>
function solution(s){
let answer="";
for(let x of s){
let num=x.charCodeAt();
if(num>=97 && num<=122) answer+=String.fromCharCode(num-32);
else answer+=x;
//if(x===x.toLowerCase()) answer+=x.toUpperCase();
//else answer+=x;
}
return answer;
}
let str="ItisTimeToStudy";
console.log(solution(str));
</script>
</body>
</html>
나랑 같은 답이 나오지만 내장함수를 다른 걸 사용했다. 이부분에 대해서는 강의를 봐야겠다. 그리고 나는 for문을 안돌았다.