머쓱이는 태어난 지 6개월 된 조카를 돌보고 있습니다. 조카는 아직 "aya", "ye", "woo", "ma" 네 가지 발음을 최대 한 번씩 사용해 조합한(이어 붙인) 발음밖에 하지 못합니다. 문자열 배열 babbling이 매개변수로 주어질 때, 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return하도록 solution 함수를 완성해주세요.
babbling | result |
---|---|
["aya", "yee", "u", "maa", "wyeoo"] | 1 |
["ayaye", "uuuma", "ye", "yemawoo", "ayaa"] | 3 |
function solution(babbling) {
const arr = ['aya', 'ye', 'woo', 'ma'];
let answer = [];
babbling.forEach(babble => {
let temp = babble;
arr.forEach(s => temp = temp.replace(s, ' '));
answer.push(temp.trim().length);
});
return answer.filter(x => x === 0).length;
}
😛 너무 헷갈려서 아래처럼 다 풀어서 쓴 다음에 변경하였다. 😛
babbling = ["ayaye", "uuuma", "ye"]; // 매개변수
arr = ['aya', 'ye', 'woo', 'ma']; // 발음
let text1 = babbling[0]; // ayaye
text1 = text1.replace(arr[0], '-'); // -ye
text1 = text1.replace(arr[1], '-'); // --
text1 = text1.replace(arr[2], '-'); // --
text1 = text1.replace(arr[3], '-'); // --
let text2 = babbling[1]; // uuuma
text2 = text2.replace(arr[0], '-'); // uuuma
text2 = text2.replace(arr[1], '-'); // uuuma
text2 = text2.replace(arr[2], '-'); // uuuma
text2 = text2.replace(arr[3], '-'); // uuu-
let text3 = babbling[2]; // ye
text3 = text3.replace(arr[0], '-'); // ye
text3 = text3.replace(arr[1], '-'); // -
text3 = text3.replace(arr[2], '-'); // -
text3 = text3.replace(arr[3], '-'); // -
// 하이픈만 남음 text1, text3 = 발음 할 수 있음 (2개)
매개변수에 발음이 포함되어 있으면 그 부분을 (-) 하이픈으로 변경하여
하이픈만 남은 값을 세어 그 수를 리턴한다.
(변경 시 하이픈은 ' ' 공백으로 바꿔서 처리함)
🤍 replace()
const str = '안녕하세요';
const newStr = str.replace('하세요', '');
console.log(str) // '안녕하세요'
console.log(newStr) // '안녕'
🤍 filter()
const filtered = [12, 5, 8, 130, 44].filter(value => value >= 10);
console.log(filtered) // [12, 130, 44]