코테연습
188. 모음사전 Javascript
hxunz
2023. 3. 16. 00:03
https://school.programmers.co.kr/learn/courses/30/lessons/84512
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
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);
// });