https://school.programmers.co.kr/learn/courses/30/lessons/84512
const nextWord = {
A: 'E',
E: 'I',
I: 'O',
O: 'U'
};
const solution = (word) => {
let w = 'A';
let count = 1;
while (true) {
if (w === word) {
return count;
}
w = run(w);
count++;
}
};
const run = (word, position = 1) => {
if (word.length < 5) {
return `${word}A`
}
const next = nextWord[word[word.length - position]];
if (!next) {
return run(word, position + 1)
};
const keepWord = word.slice(0, -position);
return keepWord + next
}
test('run', () => {
expect(run("A")).toEqual('AA');
expect(run("AA")).toEqual('AAA');
expect(run("AAA")).toEqual('AAAA');
expect(run("AAAA")).toEqual('AAAAA');
expect(run("AAAAA")).toEqual('AAAAE');
});
// test('solution', () => {
// expect(solution("AAAAE")).toEqual(6);
// });
'코테연습' 카테고리의 다른 글
190. 피로도 Javascript (0) | 2023.03.16 |
---|---|
189. 덧칠하기 Javascript (0) | 2023.03.16 |
187. 택배 배달과 수거하기 Javascript (0) | 2023.03.15 |
186. 바탕화면 정리 Javascript (0) | 2023.03.15 |
185. 캐릭터의 좌표 Javascript (0) | 2023.03.15 |
댓글