반응형
10. 문자찾기
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s, t) {
let answer = 0;
for (const ele of s) {
if (ele === t) answer++;
}
return answer;
}
let str = 'COMPUTERPROGRAMMING';
console.log(solution(str, 'R'));
</script>
</body>
</html>
11. 대문자찾기
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s) {
let answer = 0;
for (let ele of s) {
if (ele === ele.toUpperCase()) {
answer++;
}
}
return answer;
}
let str = 'KoreaTimeGood';
console.log(solution(str));
</script>
</body>
</html>
12. 대문자로 통일
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s) {
return s.toUpperCase();
}
let str = 'ItisTimeToStudy';
console.log(solution(str));
</script>
</body>
</html>
13. 대소문자 변환
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s) {
let answer = '';
for (let ele of s) {
const codeNum = ele.charCodeAt();
if (ele === ele.toUpperCase()) {
answer += ele.toLowerCase();
} else {
answer += ele.toUpperCase();
}
// if (codeNum >= 65 && codeNum <= 90) {
// answer += ele.toLowerCase();
// } else {
// answer += ele.toUpperCase();
// }
}
return answer;
}
console.log(solution('StuDY'));
</script>
</body>
</html>
14. 가장 긴 문자열
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s) {
let answer = '';
let maxLength = 0;
for (let word of s) {
if (maxLength < word.length) {
answer = word;
maxLength = word.length;
}
}
return answer;
}
let str = ['teacher', 'time', 'student', 'beautiful', 'good'];
console.log(solution(str));
</script>
</body>
</html>
15. 가운데 문자 출력
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s) {
// arr.slice([begin[, end]])
// array.splice(start[, deleteCount[, item1[, item2[,
let answer;
const mid = Math.floor(s.length / 2);
//홀수일 때
if (s.length % 2 === 1) {
answer = s.slice(mid, mid + 1);
}
//짝수일 때
else {
answer = s.slice(mid - 1, mid + 1);
}
//짝수일때
return answer;
}
console.log(solution('stu'));
</script>
</body>
</html>
16. 중복문자제거
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s) {
let answer = '';
const map = new Map();
for (const ele of s) {
if (!map.has(ele)) {
map.set(ele, true);
answer += ele;
}
}
console.log(map);
return answer;
}
console.log(solution('ksekkset'));
</script>
</body>
</html>
17. 중복단어제거
<html>
<head>
<meta charset="UTF-8" />
<title>출력결과</title>
</head>
<body>
<script>
function solution(s) {
let answer = [];
const map = new Map();
for (let word of s) {
if (!map.has(word)) {
map.set(word, true);
answer.push(word);
}
}
return answer;
}
let str = ['good', 'time', 'good', 'time', 'student'];
console.log(solution(str));
</script>
</body>
</html>
반응형
'CS (컴퓨터 사이언스) > Algorithm (알고리즘)' 카테고리의 다른 글
[알고리즘] 1. 회문문자열 / 2. 유효한 펠린드롬 / 3. 숫자만 추출 / 4. 가장짧은문자거리 / 5. 문자열압축 (0) | 2024.01.28 |
---|---|
[알고리즘] 1. 큰수출력하기 / 2. 보이는 학생 / 3. 가위바위보 / 5. 등수구하기 / 6. 격자최대합 / 7. 봉우리 (0) | 2024.01.28 |
[알고리즘] 8. 일곱난쟁이 / 9. A를 #으로 (0) | 2023.12.10 |
[알고리즘] 3.연필개수 / 4. 1부터 N까지의 합 / 6.홀수 / 7 . 10부제 (0) | 2023.12.09 |
[알고리즘] 2.삼각형 판별하기 (0) | 2023.11.25 |