FRONT/JAVASCRIPT
[Javascript] 알고리즘 기본 문제 - 대문자로 통일
연듀
2022. 3. 25. 21:38
12.대문자로 통일
내풀이
function solution(s) {
let answer = [];
for (let x of s) {
x = x.toUpperCase();
answer.push(x);
}
return answer.join("");
}
let str = "ItisTimeToStudy";
console.log(solution(str));
강사님 풀이
function solution(s) {
let answer = "";
for (let x of s) {
// 1. 아스키 코드 방법
let num = x.charCodeAt();
if (num >= 97 && num <= 122) answer += String.fromCharCode(num - 32);
// 소문자를 대문자로
else answer += x;
// 2. 일반적 방법
// if (x === x.toLowerCase()) answer += x.toUpperCase();
// else answer += x;
}
return answer;
}
let str = "ItisTimeToStudy";
console.log(solution(str));
반응형