며칠간 문제를 풀려고 시도 했지만,
부분적인 성공만을 거둬
결국 다른 사람의 코드 보고 분석해봤다...
먼저, 아기가 옹알이 할 수 있는 단어들을 배열로 저장한다.
const able = ["aya", "ye", "woo", "ma"]
매개변수로 전달 받은 문자열 배열 babblings을
forEach문으로 반복시킨다.
babblings.forEach((babbling)=>
{
})
forEach문의 element인 babbling와 스프레드 연산자(...)
를 이용해 문자열을 단어 배열로 만든다.
let charList = [...babbling]
let current = ''
let before= ''
을 만들어 현재 문자열 배열의
단어를 담을 current와
중복된 옹알이인지 확인할
before 변수를 만들어준다.
while(charList.length)
{
current += charList.shift();
if(able.includes(current)) {
if(before === current) continue;
before = current;
current = '';
}
}
단어 배열에서 shift() 함수로
current에 단어를 하나하나 담는다.
이때,
if(able.includes(current)
아기가 가능한 옹알이이고,
if(before === current) continue
중복하지 않은 옹알이이면
(
중복된 옹알이이면 continue로
아래에 있는 모든 코드를 스킵한다
)
before = current
현재 옹알이을 before에 담고
current를 초기화 시켜준다.
if(currnet === '')
currnet가 비어있으면 카운드를 증가시켜준다.
(
while문의
current += charList.shift()
에서 발음하지 못 하는 단어가
들어올 경우
current가 공백이 아니게 돼서
카운트를 증가시키지 않는다.
)
제출한 코드 :
function solution(babblings)
{
let answer = 0;
const able = ["aya", "ye", "woo", "ma"]
babblings.forEach((babbling) => {
let charList = [...babbling];
let current = '';
let before = '';
while(charList.length)
{
current += charList.shift();
if(able.includes(current)) {
if(before === current) continue;
before = current;
current = '';
}
}
if(current === '') answer++;
})
return answer;
}
'문제 풀기 > JavaScripts' 카테고리의 다른 글
문제 64. 체육복 (난이도 5) (0) | 2024.08.28 |
---|---|
문제 63. 숫자 짝꿍(난이도 7)(객체 사용 {}) (0) | 2024.08.26 |
문제 61. 로또의 최고 순위와 최저 순위(난이도 5) (0) | 2024.08.20 |
문제 60. 기사단원의 무기(난이도 5) (0) | 2024.08.20 |
문제 59. 덧칠하기(난이도 10)(new Array 와 fill) (0) | 2024.08.19 |