Skip to content

Commit f679851

Browse files
authored
Merge pull request #122 from front-studium/yujin
`Week 26` 문제 풀이
2 parents d94a587 + f01575d commit f679851

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function solution(my_string) {
2+
let answer = [];
3+
for (let i of my_string) {
4+
if (i.charCodeAt() < 58) answer.push(Number(i));
5+
}
6+
return answer.sort();
7+
}
8+
9+
// 다른 풀이
10+
// function solution(my_string) {
11+
// return [...my_string]
12+
// .filter((v) => !isNaN(v))
13+
// .map((v) => parseInt(v))
14+
// .sort();
15+
// }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function solution(my_string) {
2+
return [...my_string]
3+
.filter((char) => !isNaN(char) && char !== " ") // 숫자 문자인지 확인 (공백 제외)
4+
.reduce((acc, char) => acc + Number(char), 0); // 모두 숫자로 변환하여 누적 합산
5+
}
6+
7+
// 다른 풀이
8+
// function solution(my_string) {
9+
// // 1. 문자열에서 숫자가 아닌 모든 문자(a-z, A-Z)를 공백으로 치환
10+
// // 2. 공백을 기준으로 문자열을 분리 (split)
11+
// // 3. 빈 문자열을 걸러내고, 남은 숫자 문자열을 모두 숫자로 변환하여 합산
12+
13+
// return my_string
14+
// .replace(/[a-zA-Z]/g, " ") // 문자를 공백으로 치환
15+
// .split(" ") // 공백 기준으로 분리
16+
// .filter((v) => v !== '') // 빈 문자열 제거
17+
// .map(Number) // 숫자 배열로 변환
18+
// .reduce((acc, cur) => acc + cur, 0); // 모두 더함
19+
// }

0 commit comments

Comments
 (0)