-
Notifications
You must be signed in to change notification settings - Fork 7
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
안진우: 과제 3일차 #13
base: main
Are you sure you want to change the base?
안진우: 과제 3일차 #13
Conversation
let sum = 0 | ||
const nString = n.toString() | ||
const nArray = nString.split('') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
; 세미콜론을 습관화 해주세요
@@ -0,0 +1,4 @@ | |||
function solution(s) { | |||
let answer = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기서 answer는 필요가 없네요.
let answer = true; |
@@ -0,0 +1,4 @@ | |||
function solution(s) { | |||
let answer = true; | |||
return (s.length===4||s.length===6)&&isNaN(s)===false?true:false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return (s.length===4||s.length===6)&&isNaN(s)===false?true:false | |
return (s.length === 4 || s.length === 6) && isNaN(s) === false ? true : false |
제 눈건강과 가독성을 살려주세요
@@ -0,0 +1,9 @@ | |||
function solution(s) { | |||
let answer = true; | |||
if((s.length===4||s.length===6)&&isNaN(s)===false){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if((s.length===4||s.length===6)&&isNaN(s)===false){ | |
if ((s.length === 4 || s.length === 6) && isNaN(s) === false) { |
answer=true | ||
}else{ | ||
answer=false | ||
} | ||
return answer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
answer=true | |
}else{ | |
answer=false | |
} | |
return answer; | |
return answer = true | |
} | |
return answer = false; |
여기서 리턴을 바로 해주면 else가 필요 없어집니다. 당연히 아시겠지만 return이 실행되면 함수 실행이 끝납니다.
const nArray = nString.split('') | ||
|
||
for (let i = 0; i < nArray.length; i++){ | ||
sum += parseInt(nArray[i]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sum += parseInt(nArray[i]) | |
sum += +nArray[i] |
크게 상관은 없는데 이런 방법도 있다는걸 알려드립니다.
@@ -0,0 +1,4 @@ | |||
function solution(s) { | |||
let answer = true; | |||
return (s.length===4||s.length===6)&&isNaN(s)===false?true:false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return (s.length===4||s.length===6)&&isNaN(s)===false?true:false | |
return (s.length === 4 || s.length === 6) && !isNaN(s) |
이렇게 바꿔도 무방합니당 물론 테스트케이스 11은 통과 못할거에요 ㅋㅋㅋ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
감사합니다!
let answer = []; | ||
let cutArray = []; | ||
for (let i = 0; i < commands.length; i++){ | ||
cutArray = array.slice (commands[i][0]-1, commands[i][1]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cutArray = array.slice (commands[i][0]-1, commands[i][1]); | |
cutArray = array.slice (commands[i][0] - 1, commands[i][1]); |
let cutArray = []; | ||
for (let i = 0; i < commands.length; i++){ | ||
cutArray = array.slice (commands[i][0]-1, commands[i][1]); | ||
cutArray.sort ( (a, b) => a - b ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cutArray.sort ( (a, b) => a - b ); | |
cutArray.sort((a, b) => a - b); |
for (let i = 0; i < commands.length; i++){ | ||
cutArray = array.slice (commands[i][0]-1, commands[i][1]); | ||
cutArray.sort ( (a, b) => a - b ); | ||
answer[i] = cutArray[commands[i][2]-1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
answer[i] = cutArray[commands[i][2]-1]; | |
answer[i] = cutArray[commands[i][2] - 1]; |
let inumindex = numbers[i]; | ||
let jnumindex = numbers[j]; | ||
answer.push(inumindex + jnumindex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let inumindex = numbers[i]; | |
let jnumindex = numbers[j]; | |
answer.push(inumindex + jnumindex); | |
answer.push(numbers[i] + numbers[j]); |
불필요한 변수 선언은 하지 않는게 좋습니다.
answer.sort(function(a, b){ | ||
return a - b; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
answer.sort(function(a, b){ | |
return a - b; | |
}); | |
answer.sort((a, b) => a - b); |
k번째수보다 이게 더 먼저 푼 문제인가요? 크게 상관은 없는데 arrow function이 좀 더 깔끔하겠네요
const setAnser = [...new Set(answer)]; | ||
return setAnser; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const setAnser = [...new Set(answer)]; | |
return setAnser; | |
return [...new Set(answer)]; |
불필요한 변수선언 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
어때 배열로만 접근하니까 깔끔하지??
그래도 무조건 객체로 접근해서 풀어봐야됩니다
return participant[i]; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋습니다! 화이팅 :)
자신이 생각한 알고리즘으로 먼저 풀어보고, 추후 조금 더 나은 코드는 어떻게 짜야할지 생각한다면 앞으로도 훨씬 더 많은 도움이 됩니다!
function solution(absolutes, signs) { | ||
let sum = 0; | ||
for(let i = 0; i<absolutes.length; i++){ | ||
if(signs[i] == true){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(signs[i] == true){ | |
if(signs[i]){ |
문제를 보니까. signs는 bollean값인걸 확인했습니다!
js에서는 bollean값일 경우에는 비교연산자를 사용하지 않고도 signs[ i ] 가 true일경우와 false일 경우가 구분됩니다!
바꾸지 마시고 참고만 해두시면 좋을 것 같아요! 잘 푸셨습니다 👍
return answer; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이해하기 어려웠던 문제는, 다른 방법을 통해서 다시 풀어보는 것도 좋은 방법입니다 👍
�예시로, split함수 후에, map함수와 join을 통해서 푸는 방법도 있습니다!
참고자료 : js 메소드
https://velog.io/@yoojinpark/Javascriptmapfilter
// zeroCount값을 증가시킨다음에 count값이 6개면 다 맞았기 때문에 결과값에 1,1 을 넣어주고 | ||
// count값이 5개면 나머지 하나 값이 0인지 그냥 틀린 값인지 검사해서 0이면 1,2 를 틀린값이면 2,2를 결과값을 출력한다. | ||
// 이것을 count 6...0까지 zeroCount 값이 0...6까지 나올 수 있는 모든 경우의 수를 검사한다. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
알고리즘을 자신이 생각한 데로 모든 경우의 수를 검사하는 의지.. 너무 좋습니다! 💯
하지만, 조금 더 코드를 간결하게 구성할 수 있는 방안으로 알고리즘에 힌트를 짧게 드릴게요.
- 0을 넣기 위한 배열과, 일치값을 넣기 위한 배열을 만들어서도 풀어보세요!
- 문제를 보면, 결국 원하는 것은 최고 순위와 최저 순위입니다. 모든 케이스를 적용하면서 이해했으니. 이제 최고 순위와 최저 순위만 계산하는 로직을 짜 보면 더 간결해질 것 같아요!
let answer = []; | ||
let count = 0; | ||
let zeroCount = 0; | ||
for (let i=0; i<lottos.length; i++){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (let i=0; i<lottos.length; i++){ | |
for (let i=0; i<6; i++){ |
문제의 제한사항- 1번 항목입니다. [lottos는 길이 6인 정수 배열입니다.]
}else if(lottos[i] === 0){ | ||
zeroCount++; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기서부터 모든 케이스를 적용하는데, 이 코드를 축약시키려면 문제에서 요구하는 최대 등수와 최소 등수만 구하면 가능합니다!
count를 맞춘 개수로 하신 것 같고, zeroCount를 0의 개수로 하신 것 같은데 이 두개를 이용해서 if문 또는 for문으로 최대 등수와 최소 등수를 구해보세요 👍
let twoTester = [2, 1, 2, 3, 2, 4, 2, 5]; | ||
let threeTester = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]; | ||
let count = [0, 0, 0]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이것도 다시 한번 풀어봤으면 하는 문제입니다!
- % -> 나머지 연산자. 왜 %5, %8, %10을 하는지 떠올리세요
- let count = [0, 0, 0]처럼, tester 또한 3개의 배열로 묶을 수 있습니다
- Math.max함수를 기억하세요
let step1 = /[A-Z]/g; //대문자 찾기 | ||
let step2 = /[^a-z0-9-_.]/g; //소문자, 숫자, 빼기, 밑줄, 마침표를 제외한 문자 제거 | ||
let step3 = /\.+/g; //마침표 2개 이상이면 1개로 고정 | ||
let step4_1 = /^[\.]/g; //첫 번째 마침표 찾기 | ||
let step4_2And6 = /[\.]$/g; //마지막 마침표 찾기 | ||
let step5 = /\s/g; //공백 찾기 | ||
let step7 = /.$/g; //마지막 문자 찾기 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let step1 = /[A-Z]/g; //대문자 찾기 | |
let step2 = /[^a-z0-9-_.]/g; //소문자, 숫자, 빼기, 밑줄, 마침표를 제외한 문자 제거 | |
let step3 = /\.+/g; //마침표 2개 이상이면 1개로 고정 | |
let step4_1 = /^[\.]/g; //첫 번째 마침표 찾기 | |
let step4_2And6 = /[\.]$/g; //마지막 마침표 찾기 | |
let step5 = /\s/g; //공백 찾기 | |
let step7 = /.$/g; //마지막 문자 찾기 | |
let step1 = /[A-Z]/g, //대문자 찾기 | |
step2 = /[^a-z0-9-_.]/g, //소문자, 숫자, 빼기, 밑줄, 마침표를 제외한 문자 제거 | |
step3 = /\.+/g, //마침표 2개 이상이면 1개로 고정 | |
step4_1 = /^[\.]/g, //첫 번째 마침표 찾기 | |
step4_2And6 = /[\.]$/g, //마지막 마침표 찾기 | |
step5 = /\s/g, //공백 찾기 | |
step7 = /.$/g; //마지막 문자 찾기 |
let을 한 번만 사용하고 선언이 가능합니다.
@@ -0,0 +1,20 @@ | |||
function solution(N, stages) { | |||
let answer = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let answer = []; | |
const answer = []; |
배열이나 객체 같은 경우 배열, 객체 그 차제의 값이 바뀌는게 아니라면, 안에있는 값을 참조해서 변경하는 것이기 때문에 const
로 선언하는게 좋습니다.
원시값과, 참조값에 대해서 공부해 보시면 좋을 듯 싶습니다.
참고사이트
No description provided.