솔루션1
const convertPWordsToNum = (word) => {
const pWords = ["aya", "ye", "woo", "ma"];
return pWords.reduce((result, pWord, i) => result.replaceAll(pWord, i), word);
};
const canPronounce = (word) => {
const result = convertPWordsToNum(word);
return (
!/[^\d]/.test(result) &&
[...result].every(
(num, i) => i + 1 > result.length || num !== result[i + 1]
)
);
};
function solution(babbling) {
console.log(babbling.filter((b) => canPronounce(b)).length);
return babbling.filter((b) => canPronounce(b)).length;
}
reduce와 정규식을 이용해서 풀이
솔루션2
const solution = (babbling) => {
const can = ["aya", "ye", "woo", "ma"];
let count = 0;
for (let i = 0; i < babbling.length; i++) {
let babble = babbling[i];
for (let j = 0; j < can.length; j++) {
if (babble.includes(can[j].repeat(2))) {
break;
}
babble = babble.split(can[j]).join(" ");
}
if (babble.split(" ").join("").length === 0) {
count += 1;
}
}
console.log(count);
return count;
};
이중for문으로 풀이
파이썬이나 자바스크립트처럼 인터프리터, 스크립트 언어는 이중for문이 안좋다고 한다.
솔루션3
const solution = (babbling) => {
let answer = 0;
const regex = /^(aya|ye|woo|ma)+$/; // 정규식
babbling.forEach((word) => {
if (regex.test(word)) {
answer++;
}
});
console.log(answer);
return answer;
};
정규식으로 풀이
'프로그래머스(Javascript) > Level 0' 카테고리의 다른 글
[프로그래머스 | 자바스크립트] 잘라서 배열로 저장하기 (0) | 2023.02.04 |
---|---|
[프로그래머스 | 자바스크립트] 문자열 밀기 (0) | 2023.02.04 |
[프로그래머스 | 자바스크립트] 종이 자르기 (0) | 2023.02.04 |
[프로그래머스 | 자바스크립트] 연속된 수의 합 (0) | 2023.02.04 |
[프로그래머스 | 자바스크립트] 다음에 올 숫자 (0) | 2023.02.04 |