0부터 숫자 하나씩 늘리면서 전체 문자열 길이가 t*m을 넘을 때까지 숫자를 n진수로 치환한 문자열을 결과 문자열에 붙여 줌
ㄴ 이 때, Number.prototype.toString([base])는 9가 넘는 경우, 소문자 알파벳을 반환하므로 String.prototype.toUpperCase() 처리 필요
특정 순서에 말해야 하는 문자를 for문을 통해 String.prototype.slice([startIdx, endIdx])로 추출
function solution(n, t, m, p) {
// 1. tm 길이의 전체 문자열 n진수 변환
let all = "";
let num = 0;
while (all.length <= t m) {
all += num.toString(n);
num += 1;
}
all = all.slice(0, t m); // tm 길이로 조정
all = all.toUpperCase(); // 전체 대문자로 표시
// 2. 튜브 순서에 말해야하는 문자 추출
let result = "";
for (let i = 0; i < t * m; i += m) {
const idx = i + p - 1;
result += all.slice(idx, idx + 1);
}
return result;
}
### ✅ 2차 풀이: 구현
- while문에서 특정 진법으로 숫자 변환하며 t * m 까지만 구하기!
```js
function solution2(n, t, m, p) {
let num = 0;
let str = "";
while (str.length < m * t) {
str += num.toString(n);
num += 1;
}
let answer = "";
for (let x = 0; x < t; x++) {
answer += str[m * x + p - 1].toUpperCase();
}
return answer;
}
⭐ 성찰
❓ 문제 상황
n진수게임
👨💻 문제 해결
✅ 1차 풀이: for 문을 통한 구현
Number.prototype.toString([base])
는 9가 넘는 경우, 소문자 알파벳을 반환하므로String.prototype.toUpperCase()
처리 필요String.prototype.slice([startIdx, endIdx])
로 추출function solution(n, t, m, p) { // 1. tm 길이의 전체 문자열 n진수 변환 let all = ""; let num = 0; while (all.length <= t m) { all += num.toString(n); num += 1; } all = all.slice(0, t m); // tm 길이로 조정 all = all.toUpperCase(); // 전체 대문자로 표시
// 2. 튜브 순서에 말해야하는 문자 추출 let result = ""; for (let i = 0; i < t * m; i += m) { const idx = i + p - 1; result += all.slice(idx, idx + 1); }
return result; }