Skip to content

[풀이 추가] 2022.04.15 8문제 추가 #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion level-1/가운데-글자-가져오기.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ function solution(s) {
// 홀수인 경우 - 중간지점
else answer = s[point];
return answer;
}
}

// 정답 4 - chaerin-dev
function solution(s) {
return s.length % 2
? s[parseInt(s.length / 2)]
: s[s.length / 2 - 1] + s[s.length / 2];
}

10 changes: 9 additions & 1 deletion level-1/같은-숫자는-싫어.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ function solution(arr) {
answer[answer.length - 1] !== arr[i] ? answer.push(arr[i]) : null
}
return answer;
}
}

//정답 3 - chaerin-dev
function solution(arr) {
// 콜백함수의 조건을 만족하는 '모든' 값을 배열로 반환하고,
// 조건을 만족하는 값이 없으면 빈 배열을 반환하는 filter 메서드 활용
// 첫 번째 요소의 경우 undefined와 비교
return arr.filter((item, index) => item !== arr[index - 1]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter에 대한 간략한 설명을 적어주셔서 처음 보시는 분들도 이해하기 좋을 것 같습니다 👍

}
9 changes: 9 additions & 0 deletions level-1/나누어-떨어지는-숫자-배열.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ function solution(arr, divisor) {
answer.length === 0 ? answer.push(-1) : null
answer.sort((a, b) => a - b)
return answer;
}

//정답 3. chaerin-dev
function solution(arr, divisor) {
let answer = [];
arr.forEach((e) => {
if (e % divisor === 0) answer.push(e);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

24-27 라인이 answer = arr.filter(e => e %divisor === 0) 와 같은 동작을 하지 않을까 의견남겨봅니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러네요!! filter 메서드가 익숙하지 않아서 떠올리지 못했는데, filter를 사용하면 가독성도 좋고 훨씬 간결한 코드가 되겠네요. 감사합니다!! 👍👍👍

return answer.length ? answer.sort((a, b) => a - b) : [-1];
}
10 changes: 10 additions & 0 deletions level-1/문자열-내-p와-y의-개수.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ function solution(s) {

return p == y ? true : false;
}

//정답 3 - chaerin-dev
function solution(s) {
let pCount = s.split(/p/i).length - 1;
let yCount = s.split(/y/i).length - 1;
return pCount === yCount;
}
// 문자열에서 특정 문자의 개수를 구하려면 split을 사용하면 된다.
// Ex. "ababb".split("a") 의 결과는 ["", "b", "bb"]
// => 즉, "a"의 갯수는 3에서 1을 뺀 2
13 changes: 12 additions & 1 deletion level-1/문자열-내-마음대로-정렬하기.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@ function solution(strings, n) {
return 0
})
return answer;
}
}

//정답 2 - chaerin-dev
function solution(strings, n) {
return strings.sort((a, b) => {
// 인덱스 n번째 글자를 기준으로 오름차순 정렬
if (a[n] > b[n]) return 1;
else if (a[n] < b[n]) return -1;
// 인덱스 n번째 글자가 같으면 사전순 정렬
else return a > b ? 1 : -1;
});
}
13 changes: 12 additions & 1 deletion level-1/문자열-내림차순으로-배치하기.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@ function solution(s) {
return 0
}).join('')
return answer;
}
}

//정답 2 - chaerin-dev
function solution(s) {
// 문자열 -> 배열 -> 정렬 -> 순서뒤집기 -> 문자열
return s.split("").sort().reverse().join("");
}

//정답3 - chaerin-dev
function solution(s) {
return s.split("").sort((a, b) => (a < b ? 1 : -1)).join("");
}
13 changes: 13 additions & 0 deletions level-1/문자열-다루기-기본.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ function solution(s) {

return answer;
}

//정답 4 - chaerin-dev
function solution(s) {
// 길이가 4 혹은 6이 아니면 false 반환
if (s.length !== 4 && s.length !== 6) return false;
// 각 자리중에 숫자가 아닌 것이 하나라도 있으면 false 반환
for (let i = 0; i < s.length; i++) {
if (isNaN(Number(s[i]))) return false;
}
// 위의 모든 조건에 포함되지 않으면
// (길이가 4 혹은 6이고, 숫자로만 구성되어 있으면) true 반환
return true;
}
19 changes: 19 additions & 0 deletions level-1/소수-찾기.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,23 @@ function solution(n) {
for (const isPrime of isPrimeArr) if (isPrime) answer += 1
answer -= 2 //인덱스 0과 1 제외
return answer;
}

//정답 2 - chaerin-dev
// 소수인지 판별하는 함수
function isPrime(x) {
for (let i = 2; i <= Math.sqrt(x); i++) {
if (x % i === 0) return false;
}
return true;
}
function solution(n) {
// 소수의 개수를 저장할 변수
let answer = 0;
// 1은 소수가 아니므로 2부터 n까지 모든 수에 대해
for (let i = 2; i <= n; i++) {
// 소수이면 소수의 개수에 1 추가
if (isPrime(i)) answer++;
}
return answer;
}