본문 바로가기

Record/TIL13

TIL 20230410 알고리즘 문제풀이 function solution(n) { let arr = [] let pow = [] let acul = [] while (true) { arr.push(n % 3) n = Math.floor(n / 3) if (n / 3 === 0) { break } } for (j = arr.length - 1; j >= 0; j--) { pow.push(j) } for(i=0; i a+b) } console.log(solution(45)) console.log(solution(125)) # 숫자를 3진법으로 변환하는 과정은 다음과 같다. 1. 45 / 3 =.. 2023. 4. 13.
TIL 20230408 알고리즘 문제 풀이 function solution(s) { const words = s.split(" "); let result = ""; for (let i = 0; i < words.length; i++) { const word = words[i]; for (let j = 0; j < word.length; j++) { if (j % 2 === 0) { result += word[j].toUpperCase(); } else { result += word[j].toLowerCase(); } } if (i !== words.length - 1) { result += " "; } } return result; } console.log(solution("try hello world ")) split(" ")을 사용해 문자열로.. 2023. 4. 10.
TIL 20230407 알고리즘 문제풀이 process.stdin.setEncoding('utf8'); process.stdin.on('data', data => { const n = data.split(" "); const a = Number(n[0]), b = Number(n[1]); for(i=0; ib){ for(i=b; i a + b) } 반복문 내에 조건문을 사용해서 signs의 i번째 인덱스가 false라면 absolutes의 i 번째 인덱스에 - 1을 곱해준 후 빈배열을 할당한 newArray에 push하게 했다. 그 후 마지막으로 newArray의 값을 reduce() 함수를 사용해 더해줬다. reduce() 함수를 사용하면 배열의 요소를 순회하면서 누적값을 계산할 수 있다. 이를 통해 배열 요소들의 합, 평균, 최댓값, 최솟값.. 2023. 4. 10.
TIL 20230406 알고리즘 문제풀이 function solution(num, k) { let num1 = String(num) let k1 = String(k) let answer = num1.indexOf(k1,0) + 1 if(num1.indexOf(k1,0) === -1){ return -1 } return answer; } console.log(solution(29183,1)) console.log(solution(232443,4)) console.log(solution(123456,7)) 정수 num에 k가 해당하는 자리 수를 리턴하는 함수이다. 이때 주의해야 할 점은 index 위치와 달리 1,2,3 순서로 카운트된다. 따라서 indexOf 함수를 사용했는데 indexOf() 함수는 문자열 내에서 특정 문자열이 처음으로 등장하는.. 2023. 4. 7.